package mysql

import "context"

func (r *Repository) ensureCumulativeRechargeRewardTables(ctx context.Context) error {
	statements := []string{
		`CREATE TABLE IF NOT EXISTS cumulative_recharge_reward_configs (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  enabled BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否启用累充奖励',
		  updated_by_admin_id BIGINT NOT NULL DEFAULT 0 COMMENT '最近更新管理员',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
		  PRIMARY KEY (app_code)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='累充奖励配置'`,
		`CREATE TABLE IF NOT EXISTS cumulative_recharge_reward_tiers (
		  tier_id BIGINT NOT NULL AUTO_INCREMENT COMMENT '档位自增 ID',
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  tier_code VARCHAR(64) NOT NULL COMMENT '档位编码',
		  tier_name VARCHAR(128) NOT NULL COMMENT '档位展示名称',
		  threshold_usd_minor BIGINT NOT NULL COMMENT '达标美元金额，美分',
		  resource_group_id BIGINT NOT NULL COMMENT '达标后发放资源组 ID',
		  status VARCHAR(32) NOT NULL DEFAULT 'active' COMMENT 'active/inactive',
		  sort_order INT NOT NULL DEFAULT 0 COMMENT '展示排序',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
		  PRIMARY KEY (tier_id),
		  UNIQUE KEY uk_cumulative_recharge_reward_tier_code (app_code, tier_code),
		  KEY idx_cumulative_recharge_reward_tier_sort (app_code, status, sort_order, threshold_usd_minor)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='累充奖励档位'`,
		`CREATE TABLE IF NOT EXISTS cumulative_recharge_reward_events (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  event_id VARCHAR(128) NOT NULL COMMENT 'wallet_outbox event_id',
		  cycle_key VARCHAR(16) NOT NULL COMMENT 'UTC 自然周，如 2026-W23',
		  user_id BIGINT NOT NULL COMMENT '充值用户',
		  transaction_id VARCHAR(128) NOT NULL COMMENT '钱包交易 ID',
		  command_id VARCHAR(128) NOT NULL COMMENT '钱包命令 ID',
		  recharge_type VARCHAR(64) NOT NULL DEFAULT '' COMMENT '充值来源',
		  recharge_coin_amount BIGINT NOT NULL DEFAULT 0 COMMENT '本次到账金币',
		  qualifying_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '本次计入累充的美元美分',
		  payload_json JSON NULL COMMENT '原 wallet payload',
		  occurred_at_ms BIGINT NOT NULL COMMENT '充值发生时间，UTC epoch ms',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  PRIMARY KEY (app_code, event_id),
		  KEY idx_cumulative_recharge_reward_event_user_cycle (app_code, cycle_key, user_id, occurred_at_ms),
		  KEY idx_cumulative_recharge_reward_event_transaction (app_code, transaction_id)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='累充奖励充值事件幂等表'`,
		`CREATE TABLE IF NOT EXISTS cumulative_recharge_reward_progress (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  cycle_key VARCHAR(16) NOT NULL COMMENT 'UTC 自然周，如 2026-W23',
		  user_id BIGINT NOT NULL COMMENT '用户 ID',
		  total_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '周期累计美元美分',
		  total_coin_amount BIGINT NOT NULL DEFAULT 0 COMMENT '周期累计到账金币',
		  first_recharged_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '周期首笔充值时间',
		  last_recharged_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '周期最近充值时间',
		  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
		  PRIMARY KEY (app_code, cycle_key, user_id),
		  KEY idx_cumulative_recharge_reward_progress_total (app_code, cycle_key, total_usd_minor)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='累充奖励用户周期累计'`,
		`CREATE TABLE IF NOT EXISTS cumulative_recharge_reward_grants (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  grant_id VARCHAR(128) NOT NULL COMMENT '累充奖励发放 ID',
		  cycle_key VARCHAR(16) NOT NULL COMMENT 'UTC 自然周，如 2026-W23',
		  user_id BIGINT NOT NULL COMMENT '用户 ID',
		  event_id VARCHAR(128) NOT NULL COMMENT '触发发放的 wallet event_id',
		  transaction_id VARCHAR(128) NOT NULL COMMENT '触发发放的钱包交易 ID',
		  command_id VARCHAR(128) NOT NULL COMMENT '触发发放的钱包命令 ID',
		  tier_id BIGINT NOT NULL COMMENT '命中档位 ID',
		  tier_code VARCHAR(64) NOT NULL COMMENT '命中档位编码',
		  threshold_usd_minor BIGINT NOT NULL COMMENT '命中档位门槛，美分',
		  resource_group_id BIGINT NOT NULL COMMENT '发放资源组 ID',
		  reached_usd_minor BIGINT NOT NULL COMMENT '命中时周期累计金额，美分',
		  qualifying_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '触发事件计入金额，美分',
		  recharge_coin_amount BIGINT NOT NULL DEFAULT 0 COMMENT '触发事件到账金币',
		  recharge_type VARCHAR(64) NOT NULL DEFAULT '' COMMENT '触发事件充值来源',
		  status VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT 'pending/granted/failed',
		  wallet_command_id VARCHAR(128) NOT NULL COMMENT 'wallet 资源组发放命令',
		  wallet_grant_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'wallet 发放回执',
		  failure_reason VARCHAR(512) NOT NULL DEFAULT '' COMMENT '失败原因',
		  granted_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '发放成功时间',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
		  PRIMARY KEY (app_code, grant_id),
		  UNIQUE KEY uk_cumulative_recharge_reward_user_tier (app_code, cycle_key, user_id, tier_id),
		  UNIQUE KEY uk_cumulative_recharge_reward_wallet_command (app_code, wallet_command_id),
		  KEY idx_cumulative_recharge_reward_grant_event (app_code, event_id, status),
		  KEY idx_cumulative_recharge_reward_grant_user (app_code, cycle_key, user_id, created_at_ms),
		  KEY idx_cumulative_recharge_reward_grant_status (app_code, status, created_at_ms)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='累充奖励资源组发放记录'`,
	}
	for _, statement := range statements {
		if _, err := r.db.ExecContext(ctx, statement); err != nil {
			return err
		}
	}
	return nil
}
