-- AI Video Platform
-- MySQL 5.7+/8.0 compatible initialization script
-- Generated from docs/AI视频平台开发文档.md

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

CREATE DATABASE IF NOT EXISTS `aivideo`
  DEFAULT CHARACTER SET utf8mb4
  DEFAULT COLLATE utf8mb4_unicode_ci;

USE `aivideo`;

-- =========================================================
-- 1. User and Auth
-- =========================================================

CREATE TABLE IF NOT EXISTS `users` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `public_id` VARCHAR(64) NOT NULL COMMENT 'External public user id',
  `username` VARCHAR(64) NULL DEFAULT NULL COMMENT 'Unique username set by user',
  `nickname` VARCHAR(100) NOT NULL DEFAULT '' COMMENT 'Display nickname',
  `avatar_url` VARCHAR(500) NOT NULL DEFAULT '' COMMENT 'Avatar image url',
  `email` VARCHAR(191) NULL DEFAULT NULL,
  `mobile` VARCHAR(32) NULL DEFAULT NULL,
  `password_hash` VARCHAR(255) NOT NULL DEFAULT '',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '1=active,2=disabled,3=frozen',
  `register_ip` VARCHAR(64) NOT NULL DEFAULT '',
  `last_login_at` DATETIME NULL DEFAULT NULL,
  `last_login_ip` VARCHAR(64) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_users_public_id` (`public_id`),
  UNIQUE KEY `uk_users_username` (`username`),
  UNIQUE KEY `uk_users_email` (`email`),
  UNIQUE KEY `uk_users_mobile` (`mobile`),
  KEY `idx_users_status` (`status`),
  KEY `idx_users_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='End users';

CREATE TABLE IF NOT EXISTS `user_auths` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `auth_type` VARCHAR(32) NOT NULL COMMENT 'email/mobile/password/oauth',
  `auth_provider` VARCHAR(32) NOT NULL DEFAULT 'local',
  `auth_principal` VARCHAR(191) NOT NULL COMMENT 'Email/mobile/openid',
  `credential_hash` VARCHAR(255) NULL DEFAULT NULL,
  `verified` TINYINT NOT NULL DEFAULT 0,
  `verified_at` DATETIME NULL DEFAULT NULL,
  `last_login_at` DATETIME NULL DEFAULT NULL,
  `last_login_ip` VARCHAR(64) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_auths_principal` (`auth_type`, `auth_provider`, `auth_principal`),
  KEY `idx_user_auths_user_id` (`user_id`),
  CONSTRAINT `fk_user_auths_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='User authentication identities';

-- =========================================================
-- 2. Admin and RBAC
-- =========================================================

CREATE TABLE IF NOT EXISTS `admin_users` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(64) NOT NULL,
  `password_hash` VARCHAR(255) NOT NULL,
  `nickname` VARCHAR(100) NOT NULL DEFAULT '',
  `avatar_url` VARCHAR(500) NOT NULL DEFAULT '',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '1=active,2=disabled',
  `is_super_admin` TINYINT NOT NULL DEFAULT 0,
  `last_login_at` DATETIME NULL DEFAULT NULL,
  `last_login_ip` VARCHAR(64) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_admin_users_username` (`username`),
  KEY `idx_admin_users_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Admin backend users';

CREATE TABLE IF NOT EXISTS `roles` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `role_code` VARCHAR(64) NOT NULL,
  `role_name` VARCHAR(100) NOT NULL,
  `status` TINYINT NOT NULL DEFAULT 1,
  `description` VARCHAR(255) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_roles_role_code` (`role_code`),
  KEY `idx_roles_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='RBAC roles';

CREATE TABLE IF NOT EXISTS `permissions` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `permission_code` VARCHAR(128) NOT NULL,
  `permission_name` VARCHAR(100) NOT NULL,
  `module_name` VARCHAR(64) NOT NULL DEFAULT '',
  `description` VARCHAR(255) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_permissions_permission_code` (`permission_code`),
  KEY `idx_permissions_module_name` (`module_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='RBAC permissions';

CREATE TABLE IF NOT EXISTS `admin_user_roles` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `admin_user_id` BIGINT UNSIGNED NOT NULL,
  `role_id` BIGINT UNSIGNED NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_admin_user_roles` (`admin_user_id`, `role_id`),
  KEY `idx_admin_user_roles_role_id` (`role_id`),
  CONSTRAINT `fk_admin_user_roles_admin_user_id`
    FOREIGN KEY (`admin_user_id`) REFERENCES `admin_users` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `fk_admin_user_roles_role_id`
    FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Admin user role mapping';

CREATE TABLE IF NOT EXISTS `role_permissions` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `role_id` BIGINT UNSIGNED NOT NULL,
  `permission_id` BIGINT UNSIGNED NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_role_permissions` (`role_id`, `permission_id`),
  KEY `idx_role_permissions_permission_id` (`permission_id`),
  CONSTRAINT `fk_role_permissions_role_id`
    FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `fk_role_permissions_permission_id`
    FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Role permission mapping';

-- =========================================================
-- 3. Wallet, Rewards, Redeem, Invites
-- =========================================================

CREATE TABLE IF NOT EXISTS `wallets` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `balance_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `frozen_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `total_recharged_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `total_consumed_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `total_refunded_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_wallets_user_id` (`user_id`),
  CONSTRAINT `fk_wallets_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='User wallets';

CREATE TABLE IF NOT EXISTS `wallet_transactions` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `transaction_no` VARCHAR(64) NOT NULL,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `wallet_id` BIGINT UNSIGNED NOT NULL,
  `biz_type` VARCHAR(32) NOT NULL COMMENT 'recharge/redeem_code/signup_reward/invite_reward/freeze/consume/refund/unfreeze/manual_adjust',
  `direction` VARCHAR(16) NOT NULL COMMENT 'in/out/freeze/unfreeze',
  `amount_points` BIGINT UNSIGNED NOT NULL,
  `balance_before_points` BIGINT UNSIGNED NOT NULL,
  `balance_after_points` BIGINT UNSIGNED NOT NULL,
  `frozen_before_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `frozen_after_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `related_type` VARCHAR(32) NOT NULL DEFAULT '',
  `related_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `remark` VARCHAR(255) NOT NULL DEFAULT '',
  `operator_type` VARCHAR(16) NOT NULL DEFAULT 'system' COMMENT 'system/admin/user',
  `operator_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `extra_json` JSON NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_wallet_transactions_no` (`transaction_no`),
  KEY `idx_wallet_transactions_user_id` (`user_id`),
  KEY `idx_wallet_transactions_biz_type` (`biz_type`),
  KEY `idx_wallet_transactions_related` (`related_type`, `related_id`),
  KEY `idx_wallet_transactions_created_at` (`created_at`),
  CONSTRAINT `fk_wallet_transactions_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_wallet_transactions_wallet_id`
    FOREIGN KEY (`wallet_id`) REFERENCES `wallets` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Wallet transaction ledger';

CREATE TABLE IF NOT EXISTS `growth_reward_rules` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `rule_type` VARCHAR(32) NOT NULL COMMENT 'signup_reward/invite_reward',
  `enabled` TINYINT NOT NULL DEFAULT 0,
  `reward_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `trigger_condition` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'on_register/on_first_consume',
  `min_consume_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `remark` VARCHAR(255) NOT NULL DEFAULT '',
  `updated_by_admin_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_growth_reward_rules_type` (`rule_type`),
  KEY `idx_growth_reward_rules_enabled` (`enabled`),
  CONSTRAINT `fk_growth_reward_rules_updated_by_admin_id`
    FOREIGN KEY (`updated_by_admin_id`) REFERENCES `admin_users` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Signup and invite reward rules';

CREATE TABLE IF NOT EXISTS `redeem_codes` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `batch_no` VARCHAR(64) NOT NULL,
  `redeem_code` VARCHAR(64) NOT NULL,
  `points` BIGINT UNSIGNED NOT NULL,
  `status` VARCHAR(32) NOT NULL DEFAULT 'unused' COMMENT 'unused/used/expired/disabled',
  `used_by_user_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `wallet_transaction_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `used_ip` VARCHAR(64) NOT NULL DEFAULT '',
  `used_user_agent` VARCHAR(255) NOT NULL DEFAULT '',
  `expired_at` DATETIME NULL DEFAULT NULL,
  `used_at` DATETIME NULL DEFAULT NULL,
  `created_by_admin_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `remark` VARCHAR(255) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_redeem_codes_code` (`redeem_code`),
  KEY `idx_redeem_codes_batch_no` (`batch_no`),
  KEY `idx_redeem_codes_status` (`status`),
  KEY `idx_redeem_codes_used_by_user_id` (`used_by_user_id`),
  CONSTRAINT `fk_redeem_codes_used_by_user_id`
    FOREIGN KEY (`used_by_user_id`) REFERENCES `users` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT,
  CONSTRAINT `fk_redeem_codes_wallet_transaction_id`
    FOREIGN KEY (`wallet_transaction_id`) REFERENCES `wallet_transactions` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT,
  CONSTRAINT `fk_redeem_codes_created_by_admin_id`
    FOREIGN KEY (`created_by_admin_id`) REFERENCES `admin_users` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Redeem codes for point exchange';

CREATE TABLE IF NOT EXISTS `invite_codes` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `invite_code` VARCHAR(32) NOT NULL,
  `invite_link` VARCHAR(255) NOT NULL DEFAULT '',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '1=enabled,0=disabled',
  `is_default` TINYINT NOT NULL DEFAULT 0,
  `max_use_count` INT UNSIGNED NULL DEFAULT NULL,
  `used_count` INT UNSIGNED NOT NULL DEFAULT 0,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_invite_codes_code` (`invite_code`),
  KEY `idx_invite_codes_user_id` (`user_id`),
  KEY `idx_invite_codes_status` (`status`),
  CONSTRAINT `fk_invite_codes_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Invite codes and links';

CREATE TABLE IF NOT EXISTS `invite_relations` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `inviter_user_id` BIGINT UNSIGNED NOT NULL,
  `invitee_user_id` BIGINT UNSIGNED NOT NULL,
  `invite_code_id` BIGINT UNSIGNED NOT NULL,
  `reward_status` VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT 'pending/eligible/rewarded/invalid',
  `reward_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `first_consumed_task_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `first_consumed_at` DATETIME NULL DEFAULT NULL,
  `rewarded_at` DATETIME NULL DEFAULT NULL,
  `reward_wallet_transaction_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `register_ip` VARCHAR(64) NOT NULL DEFAULT '',
  `register_device_fingerprint` VARCHAR(128) NOT NULL DEFAULT '',
  `risk_status` VARCHAR(32) NOT NULL DEFAULT 'normal' COMMENT 'normal/suspicious/blocked',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_invite_relations_invitee_user_id` (`invitee_user_id`),
  KEY `idx_invite_relations_inviter_user_id` (`inviter_user_id`),
  KEY `idx_invite_relations_reward_status` (`reward_status`),
  KEY `idx_invite_relations_invite_code_id` (`invite_code_id`),
  CONSTRAINT `fk_invite_relations_inviter_user_id`
    FOREIGN KEY (`inviter_user_id`) REFERENCES `users` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_invite_relations_invitee_user_id`
    FOREIGN KEY (`invitee_user_id`) REFERENCES `users` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_invite_relations_invite_code_id`
    FOREIGN KEY (`invite_code_id`) REFERENCES `invite_codes` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_invite_relations_reward_wallet_transaction_id`
    FOREIGN KEY (`reward_wallet_transaction_id`) REFERENCES `wallet_transactions` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Invite relationships and reward tracking';

-- =========================================================
-- 4. Recharge and Payment
-- =========================================================

CREATE TABLE IF NOT EXISTS `recharge_plans` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL,
  `pay_amount` DECIMAL(10,2) NOT NULL,
  `point_ratio` INT UNSIGNED NOT NULL DEFAULT 100 COMMENT 'Points per 1 CNY',
  `give_points` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Base points',
  `bonus_points` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Bonus points',
  `sort_order` INT NOT NULL DEFAULT 0,
  `status` TINYINT NOT NULL DEFAULT 1,
  `start_at` DATETIME NULL DEFAULT NULL,
  `end_at` DATETIME NULL DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_recharge_plans_status` (`status`),
  KEY `idx_recharge_plans_sort_order` (`sort_order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Recharge plans';

CREATE TABLE IF NOT EXISTS `payment_channels` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `channel_code` VARCHAR(32) NOT NULL,
  `channel_name` VARCHAR(100) NOT NULL,
  `provider_type` VARCHAR(32) NOT NULL DEFAULT 'manual' COMMENT 'alipay/wechat/manual',
  `config_json` JSON NULL,
  `status` TINYINT NOT NULL DEFAULT 1,
  `sort_order` INT NOT NULL DEFAULT 0,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_payment_channels_code` (`channel_code`),
  KEY `idx_payment_channels_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Payment channels';

CREATE TABLE IF NOT EXISTS `recharge_orders` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `order_no` VARCHAR(64) NOT NULL,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `recharge_plan_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `payment_channel_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `payment_channel_code` VARCHAR(32) NOT NULL,
  `pay_amount` DECIMAL(10,2) NOT NULL,
  `point_ratio_snapshot` INT UNSIGNED NOT NULL,
  `give_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `bonus_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `arrival_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `currency` VARCHAR(16) NOT NULL DEFAULT 'CNY',
  `status` VARCHAR(32) NOT NULL COMMENT 'pending/paid/failed/cancelled/refunded',
  `third_party_order_no` VARCHAR(100) NOT NULL DEFAULT '',
  `client_ip` VARCHAR(64) NOT NULL DEFAULT '',
  `paid_at` DATETIME NULL DEFAULT NULL,
  `expired_at` DATETIME NULL DEFAULT NULL,
  `callback_payload` JSON NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_recharge_orders_order_no` (`order_no`),
  KEY `idx_recharge_orders_user_id` (`user_id`),
  KEY `idx_recharge_orders_status` (`status`),
  KEY `idx_recharge_orders_paid_at` (`paid_at`),
  CONSTRAINT `fk_recharge_orders_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_recharge_orders_recharge_plan_id`
    FOREIGN KEY (`recharge_plan_id`) REFERENCES `recharge_plans` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT,
  CONSTRAINT `fk_recharge_orders_payment_channel_id`
    FOREIGN KEY (`payment_channel_id`) REFERENCES `payment_channels` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Recharge orders';

-- =========================================================
-- 5. Providers, Models, Pricing
-- =========================================================

CREATE TABLE IF NOT EXISTS `provider_accounts` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `provider_code` VARCHAR(32) NOT NULL,
  `provider_name` VARCHAR(100) NOT NULL,
  `api_format` VARCHAR(64) NOT NULL COMMENT 'openai_official_video/seedance_video_generation',
  `base_url` VARCHAR(255) NOT NULL,
  `api_key_encrypted` TEXT NOT NULL,
  `[REDACTED] TEXT NULL,
  `webhook_secret_encrypted` TEXT NULL,
  `timeout_seconds` INT NOT NULL DEFAULT 60,
  `max_retries` INT NOT NULL DEFAULT 3,
  `status` TINYINT NOT NULL DEFAULT 1,
  `remark` VARCHAR(255) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_provider_accounts_code` (`provider_code`),
  KEY `idx_provider_accounts_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Provider accounts';

CREATE TABLE IF NOT EXISTS `provider_models` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `provider_account_id` BIGINT UNSIGNED NOT NULL,
  `model_code` VARCHAR(64) NOT NULL,
  `model_name` VARCHAR(100) NOT NULL,
  `request_content_type` VARCHAR(64) NOT NULL DEFAULT 'application/json',
  `scene_type` VARCHAR(32) NOT NULL DEFAULT 'video_generation',
  `supports_text_to_video` TINYINT NOT NULL DEFAULT 1,
  `supports_image_to_video` TINYINT NOT NULL DEFAULT 0,
  `supports_video_reference` TINYINT NOT NULL DEFAULT 0,
  `supports_audio_reference` TINYINT NOT NULL DEFAULT 0,
  `supports_generate_audio` TINYINT NOT NULL DEFAULT 0,
  `supports_remix` TINYINT NOT NULL DEFAULT 0,
  `supports_webhook` TINYINT NOT NULL DEFAULT 0,
  `min_duration` INT NOT NULL DEFAULT 4,
  `max_duration` INT NOT NULL DEFAULT 15,
  `status` TINYINT NOT NULL DEFAULT 1,
  `default_ratio` VARCHAR(20) NOT NULL DEFAULT '16:9',
  `default_resolution` VARCHAR(20) NOT NULL DEFAULT '720p',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_provider_models` (`provider_account_id`, `model_code`),
  KEY `idx_provider_models_status` (`status`),
  CONSTRAINT `fk_provider_models_provider_account_id`
    FOREIGN KEY (`provider_account_id`) REFERENCES `provider_accounts` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Provider-side video models';

CREATE TABLE IF NOT EXISTS `video_models` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `model_key` VARCHAR(64) NOT NULL,
  `model_name` VARCHAR(100) NOT NULL,
  `frontend_title` VARCHAR(100) NOT NULL,
  `frontend_description` VARCHAR(255) NOT NULL DEFAULT '',
  `default_duration_seconds` INT NOT NULL DEFAULT 8,
  `default_ratio` VARCHAR(20) NOT NULL DEFAULT '16:9',
  `default_resolution` VARCHAR(20) NOT NULL DEFAULT '720p',
  `status` TINYINT NOT NULL DEFAULT 1,
  `sort_order` INT NOT NULL DEFAULT 0,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_video_models_key` (`model_key`),
  KEY `idx_video_models_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Platform-side public video models';

CREATE TABLE IF NOT EXISTS `video_model_supplier_bindings` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `video_model_id` BIGINT UNSIGNED NOT NULL,
  `provider_model_id` BIGINT UNSIGNED NOT NULL,
  `routing_priority` INT NOT NULL DEFAULT 100,
  `is_primary` TINYINT NOT NULL DEFAULT 0,
  `status` TINYINT NOT NULL DEFAULT 1,
  `timeout_seconds_override` INT NULL DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_video_model_supplier_binding` (`video_model_id`, `provider_model_id`),
  KEY `idx_video_model_supplier_bindings_priority` (`video_model_id`, `routing_priority`),
  CONSTRAINT `fk_video_model_supplier_bindings_video_model_id`
    FOREIGN KEY (`video_model_id`) REFERENCES `video_models` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `fk_video_model_supplier_bindings_provider_model_id`
    FOREIGN KEY (`provider_model_id`) REFERENCES `provider_models` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Bindings between platform models and provider models';

CREATE TABLE IF NOT EXISTS `pricing_rules` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `rule_name` VARCHAR(100) NOT NULL,
  `video_model_id` BIGINT UNSIGNED NOT NULL,
  `billing_mode` VARCHAR(32) NOT NULL DEFAULT 'per_second',
  `points_per_second` INT UNSIGNED NOT NULL,
  `minimum_points` INT UNSIGNED NOT NULL DEFAULT 0,
  `status` TINYINT NOT NULL DEFAULT 1,
  `effective_at` DATETIME NOT NULL,
  `expired_at` DATETIME NULL DEFAULT NULL,
  `version_no` INT NOT NULL DEFAULT 1,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_pricing_rules_video_model_id` (`video_model_id`),
  KEY `idx_pricing_rules_status` (`status`),
  KEY `idx_pricing_rules_effective_at` (`effective_at`),
  CONSTRAINT `fk_pricing_rules_video_model_id`
    FOREIGN KEY (`video_model_id`) REFERENCES `video_models` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Pricing rules by platform video model';

-- =========================================================
-- 6. Assets and Video Tasks
-- =========================================================

CREATE TABLE IF NOT EXISTS `media_assets` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `asset_no` VARCHAR(64) NOT NULL,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `media_type` VARCHAR(16) NOT NULL COMMENT 'image/video/audio',
  `source_type` VARCHAR(16) NOT NULL DEFAULT 'upload' COMMENT 'upload/generated/imported',
  `original_filename` VARCHAR(255) NOT NULL,
  `mime_type` VARCHAR(100) NOT NULL DEFAULT '',
  `file_ext` VARCHAR(32) NOT NULL DEFAULT '',
  `file_size` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `storage_provider` VARCHAR(32) NOT NULL DEFAULT 'minio',
  `storage_bucket` VARCHAR(100) NOT NULL,
  `storage_key` VARCHAR(255) NOT NULL,
  `public_url` VARCHAR(500) NOT NULL DEFAULT '',
  `sha256` VARCHAR(64) NOT NULL DEFAULT '',
  `width` INT UNSIGNED NULL DEFAULT NULL,
  `height` INT UNSIGNED NULL DEFAULT NULL,
  `duration_seconds` INT UNSIGNED NULL DEFAULT NULL,
  `status` VARCHAR(32) NOT NULL DEFAULT 'active' COMMENT 'active/deleted',
  `deleted_at` DATETIME NULL DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_media_assets_asset_no` (`asset_no`),
  KEY `idx_media_assets_user_id` (`user_id`),
  KEY `idx_media_assets_media_type` (`media_type`),
  KEY `idx_media_assets_status` (`status`),
  CONSTRAINT `fk_media_assets_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Uploaded and generated media assets';

CREATE TABLE IF NOT EXISTS `video_generation_tasks` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `task_no` VARCHAR(64) NOT NULL,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `video_model_id` BIGINT UNSIGNED NOT NULL,
  `provider_account_id` BIGINT UNSIGNED NOT NULL,
  `provider_model_id` BIGINT UNSIGNED NOT NULL,
  `provider_binding_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `pricing_rule_id` BIGINT UNSIGNED NOT NULL,
  `external_task_id` VARCHAR(100) NOT NULL DEFAULT '',
  `submit_mode` VARCHAR(32) NOT NULL DEFAULT 'async',
  `task_status` VARCHAR(32) NOT NULL COMMENT 'created/queued/submitting/submitted/running/succeeded/failed/cancelled/timed_out',
  `generation_mode` VARCHAR(32) NOT NULL COMMENT 'text_to_video/image_to_video/multimodal',
  `prompt_text` TEXT NULL,
  `request_payload` JSON NOT NULL COMMENT 'Standardized local payload',
  `response_payload` JSON NULL COMMENT 'Latest provider response',
  `duration_seconds` INT NOT NULL DEFAULT 5,
  `ratio` VARCHAR(20) NOT NULL DEFAULT '16:9',
  `resolution` VARCHAR(20) NOT NULL DEFAULT '720p',
  `generate_audio` TINYINT NOT NULL DEFAULT 0,
  `estimated_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `frozen_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `final_points` BIGINT UNSIGNED NOT NULL DEFAULT 0,
  `supplier_cost_amount` DECIMAL(10,4) NOT NULL DEFAULT 0.0000,
  `supplier_cost_currency` VARCHAR(16) NOT NULL DEFAULT '',
  `result_asset_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `fail_reason` VARCHAR(500) NOT NULL DEFAULT '',
  `submitted_at` DATETIME NULL DEFAULT NULL,
  `started_at` DATETIME NULL DEFAULT NULL,
  `finished_at` DATETIME NULL DEFAULT NULL,
  `next_poll_at` DATETIME NULL DEFAULT NULL,
  `poll_count` INT NOT NULL DEFAULT 0,
  `user_visible` TINYINT NOT NULL DEFAULT 1 COMMENT 'User soft visibility flag',
  `user_deleted_at` DATETIME NULL DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_video_generation_tasks_task_no` (`task_no`),
  KEY `idx_video_generation_tasks_user_id` (`user_id`),
  KEY `idx_video_generation_tasks_status` (`task_status`),
  KEY `idx_video_generation_tasks_external_task_id` (`external_task_id`),
  KEY `idx_video_generation_tasks_next_poll_at` (`next_poll_at`),
  KEY `idx_video_generation_tasks_user_visible` (`user_id`, `user_visible`, `created_at`),
  CONSTRAINT `fk_video_generation_tasks_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_video_generation_tasks_video_model_id`
    FOREIGN KEY (`video_model_id`) REFERENCES `video_models` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_video_generation_tasks_provider_account_id`
    FOREIGN KEY (`provider_account_id`) REFERENCES `provider_accounts` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_video_generation_tasks_provider_model_id`
    FOREIGN KEY (`provider_model_id`) REFERENCES `provider_models` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_video_generation_tasks_provider_binding_id`
    FOREIGN KEY (`provider_binding_id`) REFERENCES `video_model_supplier_bindings` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT,
  CONSTRAINT `fk_video_generation_tasks_pricing_rule_id`
    FOREIGN KEY (`pricing_rule_id`) REFERENCES `pricing_rules` (`id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_video_generation_tasks_result_asset_id`
    FOREIGN KEY (`result_asset_id`) REFERENCES `media_assets` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Video generation tasks';

CREATE TABLE IF NOT EXISTS `video_task_events` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `video_task_id` BIGINT UNSIGNED NOT NULL,
  `event_type` VARCHAR(32) NOT NULL,
  `event_message` VARCHAR(255) NOT NULL DEFAULT '',
  `payload` JSON NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_video_task_events_video_task_id` (`video_task_id`),
  KEY `idx_video_task_events_event_type` (`event_type`),
  CONSTRAINT `fk_video_task_events_video_task_id`
    FOREIGN KEY (`video_task_id`) REFERENCES `video_generation_tasks` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Video task event timeline';

CREATE TABLE IF NOT EXISTS `callback_logs` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `source_type` VARCHAR(32) NOT NULL COMMENT 'payment/provider',
  `source_code` VARCHAR(32) NOT NULL,
  `related_no` VARCHAR(64) NOT NULL DEFAULT '',
  `request_headers` JSON NULL,
  `request_body` JSON NULL,
  `verify_status` VARCHAR(32) NOT NULL DEFAULT 'pending',
  `process_status` VARCHAR(32) NOT NULL DEFAULT 'pending',
  `response_body` TEXT NULL,
  `error_message` VARCHAR(500) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_callback_logs_source` (`source_type`, `source_code`),
  KEY `idx_callback_logs_related_no` (`related_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Inbound callback logs';

-- =========================================================
-- 7. System Config and Audit
-- =========================================================

CREATE TABLE IF NOT EXISTS `system_configs` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `config_key` VARCHAR(128) NOT NULL,
  `config_value` TEXT NOT NULL,
  `value_type` VARCHAR(32) NOT NULL DEFAULT 'string',
  `group_name` VARCHAR(64) NOT NULL DEFAULT 'default',
  `description` VARCHAR(255) NOT NULL DEFAULT '',
  `is_public` TINYINT NOT NULL DEFAULT 0,
  `updated_by_admin_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_system_configs_key` (`config_key`),
  KEY `idx_system_configs_group_name` (`group_name`),
  CONSTRAINT `fk_system_configs_updated_by_admin_id`
    FOREIGN KEY (`updated_by_admin_id`) REFERENCES `admin_users` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='System config key-value pairs';

CREATE TABLE IF NOT EXISTS `operation_logs` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `admin_user_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `user_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `module_name` VARCHAR(64) NOT NULL DEFAULT '',
  `action_name` VARCHAR(64) NOT NULL DEFAULT '',
  `related_type` VARCHAR(32) NOT NULL DEFAULT '',
  `related_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `request_method` VARCHAR(16) NOT NULL DEFAULT '',
  `request_path` VARCHAR(255) NOT NULL DEFAULT '',
  `request_ip` VARCHAR(64) NOT NULL DEFAULT '',
  `user_agent` VARCHAR(255) NOT NULL DEFAULT '',
  `request_body` JSON NULL,
  `response_body` JSON NULL,
  `status` VARCHAR(32) NOT NULL DEFAULT 'success',
  `error_message` VARCHAR(500) NOT NULL DEFAULT '',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_operation_logs_admin_user_id` (`admin_user_id`),
  KEY `idx_operation_logs_user_id` (`user_id`),
  KEY `idx_operation_logs_module_name` (`module_name`),
  KEY `idx_operation_logs_created_at` (`created_at`),
  CONSTRAINT `fk_operation_logs_admin_user_id`
    FOREIGN KEY (`admin_user_id`) REFERENCES `admin_users` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT,
  CONSTRAINT `fk_operation_logs_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Admin and system operation logs';

-- =========================================================
-- 8. Seed Data
-- =========================================================

INSERT INTO `roles` (`role_code`, `role_name`, `status`, `description`)
VALUES
  ('super_admin', '超级管理员', 1, '拥有所有后台权限'),
  ('operator', '运营管理员', 1, '负责配置、奖励和订单处理')
ON DUPLICATE KEY UPDATE
  `role_name` = VALUES(`role_name`),
  `status` = VALUES(`status`),
  `description` = VALUES(`description`);

INSERT INTO `permissions` (`permission_code`, `permission_name`, `module_name`, `description`)
VALUES
  ('user:view', '查看用户', 'user', '查看用户列表与详情'),
  ('user:edit', '编辑用户', 'user', '编辑用户状态和资料'),
  ('order:view', '查看订单', 'order', '查看充值订单'),
  ('wallet:view', '查看钱包', 'wallet', '查看用户积分'),
  ('wallet:manual_adjust', '手工调账', 'wallet', '人工调整积分'),
  ('task:view', '查看任务', 'task', '查看视频任务'),
  ('task:retry', '重试任务', 'task', '后台重试视频任务'),
  ('provider:view', '查看供应商', 'provider', '查看供应商账号和模型'),
  ('provider:edit', '编辑供应商', 'provider', '编辑供应商账号和模型'),
  ('pricing:view', '查看价格', 'pricing', '查看价格规则'),
  ('pricing:edit', '编辑价格', 'pricing', '编辑价格规则'),
  ('growth:view', '查看增长规则', 'growth', '查看注册奖励和邀请奖励'),
  ('growth:edit', '编辑增长规则', 'growth', '编辑注册奖励和邀请奖励'),
  ('redeem:view', '查看兑换码', 'redeem', '查看兑换码和兑换记录'),
  ('redeem:edit', '编辑兑换码', 'redeem', '创建和禁用兑换码'),
  ('system:view', '查看系统配置', 'system', '查看系统配置'),
  ('system:edit', '编辑系统配置', 'system', '编辑系统配置')
ON DUPLICATE KEY UPDATE
  `permission_name` = VALUES(`permission_name`),
  `module_name` = VALUES(`module_name`),
  `description` = VALUES(`description`);

INSERT IGNORE INTO `role_permissions` (`role_id`, `permission_id`)
SELECT r.`id`, p.`id`
FROM `roles` r
JOIN `permissions` p
WHERE r.`role_code` = 'super_admin';

INSERT INTO `growth_reward_rules` (`rule_type`, `enabled`, `reward_points`, `trigger_condition`, `min_consume_points`, `remark`)
VALUES
  ('signup_reward', 0, 0, 'on_register', 0, '新用户注册送积分'),
  ('invite_reward', 0, 0, 'on_first_consume', 100, '邀请奖励积分')
ON DUPLICATE KEY UPDATE
  `enabled` = VALUES(`enabled`),
  `reward_points` = VALUES(`reward_points`),
  `trigger_condition` = VALUES(`trigger_condition`),
  `min_consume_points` = VALUES(`min_consume_points`),
  `remark` = VALUES(`remark`);

INSERT INTO `payment_channels` (`channel_code`, `channel_name`, `provider_type`, `config_json`, `status`, `sort_order`)
VALUES
  ('alipay', '支付宝', 'alipay', JSON_OBJECT(), 1, 10),
  ('wechat_pay', '微信支付', 'wechat', JSON_OBJECT(), 1, 20),
  ('manual', '人工充值', 'manual', JSON_OBJECT(), 0, 99)
ON DUPLICATE KEY UPDATE
  `channel_name` = VALUES(`channel_name`),
  `provider_type` = VALUES(`provider_type`),
  `status` = VALUES(`status`),
  `sort_order` = VALUES(`sort_order`);

INSERT INTO `system_configs` (`config_key`, `config_value`, `value_type`, `group_name`, `description`, `is_public`)
VALUES
  ('site.title', 'AI Video Platform', 'string', 'site', '站点标题', 1),
  ('site.notice', '', 'string', 'site', '站点公告', 1),
  ('wallet.point_exchange_ratio', '100', 'int', 'wallet', '1元兑换多少积分', 0),
  ('reward.signup.enabled', '0', 'bool', 'reward', '是否开启注册赠送积分', 0),
  ('reward.signup.points', '0', 'int', 'reward', '注册赠送积分数量', 0),
  ('reward.invite.enabled', '0', 'bool', 'reward', '是否开启邀请奖励', 0),
  ('reward.invite.points', '0', 'int', 'reward', '邀请奖励积分数量', 0),
  ('reward.invite.trigger', 'on_first_consume', 'string', 'reward', '邀请奖励触发条件', 0),
  ('reward.invite.min_consume_points', '100', 'int', 'reward', '邀请奖励最小有效消费积分', 0),
  ('invite.code.enabled', '1', 'bool', 'invite', '是否允许用户生成邀请码', 0),
  ('upload.max_image_size_mb', '30', 'int', 'upload', '图片最大上传大小MB', 0),
  ('upload.max_video_size_mb', '50', 'int', 'upload', '视频最大上传大小MB', 0),
  ('upload.max_audio_size_mb', '15', 'int', 'upload', '音频最大上传大小MB', 0),
  ('task.default_poll_interval_seconds', '10', 'int', 'task', '默认轮询间隔秒数', 0),
  ('task.max_poll_minutes', '30', 'int', 'task', '最大轮询分钟数', 0),
  ('task.daily_create_limit', '50', 'int', 'task', '单用户每日任务上限', 0)
ON DUPLICATE KEY UPDATE
  `config_value` = VALUES(`config_value`),
  `value_type` = VALUES(`value_type`),
  `group_name` = VALUES(`group_name`),
  `description` = VALUES(`description`),
  `is_public` = VALUES(`is_public`);

SET FOREIGN_KEY_CHECKS = 1;
