-- Generated by aether-data-schema from schema/logical/*.toml.
-- Do not edit generated files directly; edit logical schema or explicit overrides instead.

CREATE TABLE IF NOT EXISTS wallets (
    `id` VARCHAR(64) NOT NULL,
    `user_id` VARCHAR(64),
    `api_key_id` VARCHAR(64),
    `balance` DOUBLE NOT NULL DEFAULT 0,
    `gift_balance` DOUBLE NOT NULL DEFAULT 0,
    `limit_mode` VARCHAR(64) NOT NULL DEFAULT 'finite',
    `currency` VARCHAR(16) NOT NULL DEFAULT 'USD',
    `status` VARCHAR(64) NOT NULL DEFAULT 'active',
    `total_recharged` DOUBLE NOT NULL DEFAULT 0,
    `total_consumed` DOUBLE NOT NULL DEFAULT 0,
    `total_refunded` DOUBLE NOT NULL DEFAULT 0,
    `total_adjusted` DOUBLE NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY wallets_user_id_key (`user_id`),
    UNIQUE KEY wallets_api_key_id_key (`api_key_id`),
    KEY wallets_api_key_id_idx (`api_key_id`),
    KEY wallets_user_id_idx (`user_id`)
);

CREATE TABLE IF NOT EXISTS wallet_transactions (
    `id` VARCHAR(64) NOT NULL,
    `wallet_id` VARCHAR(64) NOT NULL,
    `category` VARCHAR(64) NOT NULL,
    `reason_code` VARCHAR(64) NOT NULL,
    `amount` DOUBLE NOT NULL,
    `balance_before` DOUBLE NOT NULL,
    `balance_after` DOUBLE NOT NULL,
    `recharge_balance_before` DOUBLE NOT NULL,
    `recharge_balance_after` DOUBLE NOT NULL,
    `gift_balance_before` DOUBLE NOT NULL,
    `gift_balance_after` DOUBLE NOT NULL,
    `link_type` VARCHAR(64),
    `link_id` VARCHAR(128),
    `operator_id` VARCHAR(64),
    `description` LONGTEXT,
    `created_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    KEY idx_wallet_tx_wallet_created (`wallet_id`, `created_at`),
    KEY idx_wallet_tx_category_created (`category`, `created_at`),
    KEY idx_wallet_tx_reason_created (`reason_code`, `created_at`),
    KEY idx_wallet_tx_link (`link_type`, `link_id`),
    KEY ix_wallet_transactions_operator_id (`operator_id`)
);

CREATE TABLE IF NOT EXISTS wallet_daily_usage_ledgers (
    `id` VARCHAR(64) NOT NULL,
    `wallet_id` VARCHAR(64) NOT NULL,
    `billing_date` VARCHAR(16) NOT NULL,
    `billing_timezone` VARCHAR(64) NOT NULL,
    `total_cost_usd` DOUBLE NOT NULL DEFAULT 0,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_creation_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_read_tokens` BIGINT NOT NULL DEFAULT 0,
    `first_finalized_at` BIGINT,
    `last_finalized_at` BIGINT,
    `aggregated_at` BIGINT NOT NULL,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    KEY idx_wallet_daily_usage_wallet_date (`wallet_id`, `billing_timezone`, `billing_date`)
);

CREATE TABLE IF NOT EXISTS payment_orders (
    `id` VARCHAR(64) NOT NULL,
    `order_no` VARCHAR(128) NOT NULL,
    `wallet_id` VARCHAR(64) NOT NULL,
    `user_id` VARCHAR(64),
    `amount_usd` DOUBLE NOT NULL,
    `pay_amount` DOUBLE,
    `pay_currency` VARCHAR(16),
    `exchange_rate` DOUBLE,
    `refunded_amount_usd` DOUBLE NOT NULL DEFAULT 0,
    `refundable_amount_usd` DOUBLE NOT NULL DEFAULT 0,
    `payment_method` VARCHAR(64) NOT NULL,
    `payment_provider` VARCHAR(64),
    `payment_channel` VARCHAR(64),
    `order_kind` VARCHAR(64) NOT NULL DEFAULT 'wallet_recharge',
    `product_id` VARCHAR(64),
    `product_snapshot` JSON,
    `fulfillment_status` VARCHAR(64) NOT NULL DEFAULT 'pending',
    `fulfillment_error` LONGTEXT,
    `gateway_order_id` VARCHAR(128),
    `gateway_response` JSON,
    `status` VARCHAR(64) NOT NULL DEFAULT 'pending',
    `created_at` BIGINT NOT NULL,
    `paid_at` BIGINT,
    `credited_at` BIGINT,
    `expires_at` BIGINT,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_payment_orders_order_no (`order_no`),
    KEY idx_payment_orders_wallet_created (`wallet_id`, `created_at`),
    KEY idx_payment_orders_user_created (`user_id`, `created_at`),
    KEY idx_payment_orders_status (`status`),
    KEY idx_payment_orders_gateway_order_id (`gateway_order_id`),
    KEY idx_payment_orders_kind_status (`order_kind`, `status`),
    KEY idx_payment_orders_product (`product_id`)
);

CREATE TABLE IF NOT EXISTS user_invite_codes (
    `user_id` VARCHAR(64) NOT NULL,
    `invite_code` VARCHAR(64) NOT NULL,
    `active` TINYINT(1) NOT NULL DEFAULT 1,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`user_id`),
    UNIQUE KEY user_invite_codes_invite_code_key (`invite_code`),
    CONSTRAINT user_invite_codes_user_id_fkey FOREIGN KEY (`user_id`) REFERENCES users (`id`) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS user_referrals (
    `id` VARCHAR(64) NOT NULL,
    `inviter_user_id` VARCHAR(64) NOT NULL,
    `invitee_user_id` VARCHAR(64) NOT NULL,
    `invite_code_snapshot` VARCHAR(64) NOT NULL,
    `source_json` JSON,
    `first_paid_order_id` VARCHAR(64),
    `first_paid_at` BIGINT,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY user_referrals_invitee_user_id_key (`invitee_user_id`),
    KEY idx_user_referrals_inviter (`inviter_user_id`, `created_at`),
    KEY idx_user_referrals_created (`created_at`),
    KEY idx_user_referrals_invite_code (`invite_code_snapshot`),
    CONSTRAINT user_referrals_inviter_user_id_fkey FOREIGN KEY (`inviter_user_id`) REFERENCES users (`id`) ON DELETE CASCADE,
    CONSTRAINT user_referrals_invitee_user_id_fkey FOREIGN KEY (`invitee_user_id`) REFERENCES users (`id`) ON DELETE CASCADE,
    CONSTRAINT user_referrals_first_paid_order_fkey FOREIGN KEY (`first_paid_order_id`) REFERENCES payment_orders (`id`) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS referral_rewards (
    `id` VARCHAR(64) NOT NULL,
    `referral_id` VARCHAR(64) NOT NULL,
    `inviter_user_id` VARCHAR(64) NOT NULL,
    `invitee_user_id` VARCHAR(64) NOT NULL,
    `reward_type` VARCHAR(32) NOT NULL,
    `trigger_point` VARCHAR(64) NOT NULL,
    `source_order_id` VARCHAR(64),
    `idempotency_key` VARCHAR(128) NOT NULL,
    `amount_usd` DOUBLE NOT NULL,
    `status` VARCHAR(32) NOT NULL DEFAULT 'pending',
    `wallet_transaction_id` VARCHAR(64),
    `reversed_amount_usd` DOUBLE NOT NULL DEFAULT 0,
    `pending_reversal_amount_usd` DOUBLE NOT NULL DEFAULT 0,
    `failure_reason` LONGTEXT,
    `admin_operator_id` VARCHAR(64),
    `admin_note` LONGTEXT,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY referral_rewards_idempotency_key_key (`idempotency_key`),
    KEY idx_referral_rewards_inviter_status (`inviter_user_id`, `status`, `created_at`),
    KEY idx_referral_rewards_inviter_created (`inviter_user_id`, `created_at`),
    KEY idx_referral_rewards_created (`created_at`),
    KEY idx_referral_rewards_source_order (`source_order_id`),
    CONSTRAINT referral_rewards_referral_id_fkey FOREIGN KEY (`referral_id`) REFERENCES user_referrals (`id`) ON DELETE CASCADE,
    CONSTRAINT referral_rewards_inviter_user_id_fkey FOREIGN KEY (`inviter_user_id`) REFERENCES users (`id`) ON DELETE CASCADE,
    CONSTRAINT referral_rewards_invitee_user_id_fkey FOREIGN KEY (`invitee_user_id`) REFERENCES users (`id`) ON DELETE CASCADE,
    CONSTRAINT referral_rewards_source_order_fkey FOREIGN KEY (`source_order_id`) REFERENCES payment_orders (`id`) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS payment_gateway_configs (
    `provider` VARCHAR(64) NOT NULL,
    `enabled` TINYINT(1) NOT NULL DEFAULT 0,
    `endpoint_url` VARCHAR(512) NOT NULL,
    `callback_base_url` VARCHAR(512),
    `merchant_id` VARCHAR(128) NOT NULL,
    `merchant_key_encrypted` LONGTEXT,
    `pay_currency` VARCHAR(16) NOT NULL DEFAULT 'CNY',
    `usd_exchange_rate` DOUBLE NOT NULL DEFAULT 7.2,
    `min_recharge_usd` DOUBLE NOT NULL DEFAULT 1,
    `channels_json` JSON,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`provider`)
);

CREATE TABLE IF NOT EXISTS payment_callbacks (
    `id` VARCHAR(64) NOT NULL,
    `payment_order_id` VARCHAR(64),
    `payment_method` VARCHAR(64) NOT NULL,
    `callback_key` VARCHAR(128) NOT NULL,
    `order_no` VARCHAR(128),
    `gateway_order_id` VARCHAR(128),
    `payload_hash` VARCHAR(128),
    `signature_valid` TINYINT(1) NOT NULL DEFAULT 0,
    `status` VARCHAR(64) NOT NULL DEFAULT 'received',
    `payload` JSON,
    `error_message` LONGTEXT,
    `created_at` BIGINT NOT NULL,
    `processed_at` BIGINT,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_payment_callbacks_callback_key (`callback_key`),
    KEY idx_payment_callbacks_order (`order_no`),
    KEY idx_payment_callbacks_gateway_order (`gateway_order_id`),
    KEY idx_payment_callbacks_created (`created_at`),
    KEY ix_payment_callbacks_payment_order_id (`payment_order_id`)
);

CREATE TABLE IF NOT EXISTS billing_plans (
    `id` VARCHAR(64) NOT NULL,
    `title` VARCHAR(128) NOT NULL,
    `description` LONGTEXT,
    `price_amount` DOUBLE NOT NULL,
    `price_currency` VARCHAR(16) NOT NULL DEFAULT 'CNY',
    `duration_unit` VARCHAR(32) NOT NULL,
    `duration_value` BIGINT NOT NULL,
    `enabled` TINYINT(1) NOT NULL DEFAULT 1,
    `sort_order` BIGINT NOT NULL DEFAULT 0,
    `max_active_per_user` BIGINT NOT NULL DEFAULT 1,
    `purchase_limit_scope` VARCHAR(32) NOT NULL DEFAULT 'active_period',
    `entitlements_json` JSON NOT NULL,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    KEY idx_billing_plans_enabled_sort (`enabled`, `sort_order`)
);

CREATE TABLE IF NOT EXISTS user_plan_entitlements (
    `id` VARCHAR(64) NOT NULL,
    `user_id` VARCHAR(64) NOT NULL,
    `plan_id` VARCHAR(64) NOT NULL,
    `payment_order_id` VARCHAR(64) NOT NULL,
    `status` VARCHAR(64) NOT NULL DEFAULT 'active',
    `starts_at` BIGINT NOT NULL,
    `expires_at` BIGINT NOT NULL,
    `entitlements_snapshot` JSON NOT NULL,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    KEY idx_user_plan_entitlements_user_active (`user_id`, `status`, `expires_at`),
    KEY idx_user_plan_entitlements_order (`payment_order_id`)
);

CREATE TABLE IF NOT EXISTS entitlement_usage_ledgers (
    `id` VARCHAR(64) NOT NULL,
    `user_entitlement_id` VARCHAR(64) NOT NULL,
    `user_id` VARCHAR(64) NOT NULL,
    `request_id` VARCHAR(128) NOT NULL,
    `amount_usd` DOUBLE NOT NULL,
    `balance_before` DOUBLE NOT NULL,
    `balance_after` DOUBLE NOT NULL,
    `usage_date` VARCHAR(16) NOT NULL,
    `created_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_entitlement_usage_request (`user_entitlement_id`, `request_id`),
    KEY idx_entitlement_usage_user_date (`user_id`, `usage_date`),
    KEY idx_entitlement_usage_entitlement_date (`user_entitlement_id`, `usage_date`)
);

CREATE TABLE IF NOT EXISTS refund_requests (
    `id` VARCHAR(64) NOT NULL,
    `refund_no` VARCHAR(128) NOT NULL,
    `wallet_id` VARCHAR(64) NOT NULL,
    `user_id` VARCHAR(64),
    `payment_order_id` VARCHAR(64),
    `source_type` VARCHAR(64) NOT NULL,
    `source_id` VARCHAR(128),
    `refund_mode` VARCHAR(64) NOT NULL,
    `amount_usd` DOUBLE NOT NULL,
    `status` VARCHAR(64) NOT NULL DEFAULT 'pending_approval',
    `reason` LONGTEXT,
    `requested_by` VARCHAR(64),
    `approved_by` VARCHAR(64),
    `processed_by` VARCHAR(64),
    `gateway_refund_id` VARCHAR(128),
    `payout_method` VARCHAR(64),
    `payout_reference` VARCHAR(255),
    `payout_proof` JSON,
    `failure_reason` LONGTEXT,
    `idempotency_key` VARCHAR(128),
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    `processed_at` BIGINT,
    `completed_at` BIGINT,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_refund_requests_refund_no (`refund_no`),
    UNIQUE KEY uq_refund_requests_idempotency_key (`idempotency_key`),
    KEY idx_refund_wallet_created (`wallet_id`, `created_at`),
    KEY idx_refund_user_created (`user_id`, `created_at`),
    KEY idx_refund_status (`status`),
    KEY ix_refund_requests_payment_order_id (`payment_order_id`),
    KEY ix_refund_requests_requested_by (`requested_by`),
    KEY ix_refund_requests_approved_by (`approved_by`),
    KEY ix_refund_requests_processed_by (`processed_by`)
);

CREATE TABLE IF NOT EXISTS redeem_code_batches (
    `id` VARCHAR(64) NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `amount_usd` DOUBLE NOT NULL,
    `currency` VARCHAR(16) NOT NULL DEFAULT 'USD',
    `balance_bucket` VARCHAR(64) NOT NULL DEFAULT 'gift',
    `total_count` INT NOT NULL,
    `status` VARCHAR(64) NOT NULL DEFAULT 'active',
    `description` LONGTEXT,
    `created_by` VARCHAR(64),
    `expires_at` BIGINT,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    KEY idx_redeem_code_batches_status (`status`, `created_at`)
);

CREATE TABLE IF NOT EXISTS redeem_codes (
    `id` VARCHAR(64) NOT NULL,
    `batch_id` VARCHAR(64) NOT NULL,
    `code_hash` VARCHAR(128) NOT NULL,
    `code_prefix` VARCHAR(16) NOT NULL,
    `code_suffix` VARCHAR(16) NOT NULL,
    `status` VARCHAR(64) NOT NULL DEFAULT 'active',
    `redeemed_by_user_id` VARCHAR(64),
    `redeemed_wallet_id` VARCHAR(64),
    `redeemed_payment_order_id` VARCHAR(64),
    `redeemed_at` BIGINT,
    `disabled_by` VARCHAR(64),
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_redeem_codes_code_hash (`code_hash`),
    KEY idx_redeem_codes_batch_created (`batch_id`, `created_at`),
    KEY idx_redeem_codes_status (`status`, `updated_at`),
    KEY idx_redeem_codes_redeemed_user (`redeemed_by_user_id`, `redeemed_at`),
    KEY idx_redeem_codes_redeemed_order (`redeemed_payment_order_id`)
);

