package mysql

import (
	"context"

	"hyapp/pkg/xerr"
)

func (r *Repository) ensureWheelTables(ctx context.Context) error {
	if r == nil || r.db == nil {
		return xerr.New(xerr.Unavailable, "mysql repository is not configured")
	}
	statements := []string{
		`CREATE TABLE IF NOT EXISTS wheel_rule_versions (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  wheel_id VARCHAR(96) NOT NULL COMMENT '转盘 ID',
		  rule_version BIGINT NOT NULL COMMENT '不可变规则版本',
		  enabled TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否启用',
		  draw_price_coins BIGINT NOT NULL COMMENT '单抽价格，金币',
		  target_rtp_ppm BIGINT NOT NULL COMMENT '金币和礼物 RTP 目标，ppm',
		  pool_rate_ppm BIGINT NOT NULL COMMENT '每笔消耗进入转盘基础奖池比例，ppm',
		  settlement_window_draws BIGINT NOT NULL COMMENT 'RTP 观察窗口抽数',
		  initial_pool_coins BIGINT NOT NULL DEFAULT 0 COMMENT '初始奖池水位',
		  pool_reserve_coins BIGINT NOT NULL DEFAULT 0 COMMENT '奖池安全水位',
		  max_single_rtp_payout BIGINT NOT NULL DEFAULT 0 COMMENT '单次计入 RTP 的最大返奖',
		  effective_from_ms BIGINT NOT NULL COMMENT '生效时间，UTC epoch ms',
		  created_by_admin_id BIGINT NOT NULL DEFAULT 0 COMMENT '发布人',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  PRIMARY KEY (app_code, wheel_id, rule_version),
		  KEY idx_wheel_rule_versions_latest (app_code, wheel_id, rule_version),
		  KEY idx_wheel_rule_versions_enabled (app_code, enabled, created_at_ms)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转盘规则版本表'`,
		`CREATE TABLE IF NOT EXISTS wheel_prize_tiers (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  wheel_id VARCHAR(96) NOT NULL COMMENT '转盘 ID',
		  rule_version BIGINT NOT NULL COMMENT '规则版本',
		  tier_id VARCHAR(96) NOT NULL COMMENT '奖档 ID',
		  display_name VARCHAR(128) NOT NULL DEFAULT '' COMMENT '展示名称快照',
		  reward_type VARCHAR(32) NOT NULL COMMENT 'coin/gift/prop/dress',
		  reward_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '礼物或道具资源 ID',
		  reward_count BIGINT NOT NULL DEFAULT 1 COMMENT '奖励数量',
		  reward_coins BIGINT NOT NULL DEFAULT 0 COMMENT '金币奖励金额',
		  rtp_value_coins BIGINT NOT NULL DEFAULT 0 COMMENT '计入 RTP 的奖励价值；道具装扮强制为 0',
		  weight_ppm BIGINT NOT NULL COMMENT '随机权重，ppm 口径',
		  enabled TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
		  metadata_json JSON NOT NULL COMMENT '奖品展示和发放扩展快照',
		  PRIMARY KEY (app_code, wheel_id, rule_version, tier_id),
		  KEY idx_wheel_prize_tiers_rule (app_code, wheel_id, rule_version)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转盘奖档表'`,
		`CREATE TABLE IF NOT EXISTS wheel_rtp_windows (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  wheel_id VARCHAR(96) NOT NULL COMMENT '转盘 ID',
		  window_index BIGINT NOT NULL COMMENT 'RTP 窗口序号',
		  target_rtp_ppm BIGINT NOT NULL COMMENT '窗口目标 RTP',
		  control_window_draws BIGINT NOT NULL COMMENT '窗口抽数',
		  paid_draws BIGINT NOT NULL DEFAULT 0 COMMENT '已抽次数',
		  wager_coins BIGINT NOT NULL DEFAULT 0 COMMENT '消耗金币',
		  target_payout_coins BIGINT NOT NULL DEFAULT 0 COMMENT '目标返奖',
		  actual_rtp_value_coins BIGINT NOT NULL DEFAULT 0 COMMENT '实际计入 RTP 的返奖',
		  carry_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '整数除法余数',
		  status VARCHAR(32) NOT NULL DEFAULT 'open' COMMENT 'open/closed',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
		  PRIMARY KEY (app_code, wheel_id, window_index),
		  KEY idx_wheel_rtp_windows_open (app_code, wheel_id, status, window_index)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转盘 RTP 观察窗口'`,
		`CREATE TABLE IF NOT EXISTS wheel_pools (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  wheel_id VARCHAR(96) NOT NULL COMMENT '转盘 ID',
		  balance BIGINT NOT NULL DEFAULT 0 COMMENT '当前可用水位',
		  reserve_floor BIGINT NOT NULL DEFAULT 0 COMMENT '安全水位',
		  total_in BIGINT NOT NULL DEFAULT 0 COMMENT '累计入池',
		  total_out BIGINT NOT NULL DEFAULT 0 COMMENT '累计 RTP 价值支出',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
		  PRIMARY KEY (app_code, wheel_id)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转盘基础奖池'`,
		`CREATE TABLE IF NOT EXISTS wheel_draw_records (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  draw_id VARCHAR(128) NOT NULL COMMENT '抽奖 ID',
		  command_id VARCHAR(128) NOT NULL COMMENT '幂等命令 ID',
		  user_id BIGINT NOT NULL COMMENT '用户 ID',
		  device_id VARCHAR(128) NOT NULL COMMENT '设备 ID',
		  visible_region_id BIGINT NOT NULL DEFAULT 0 COMMENT '可见区域 ID',
		  wheel_id VARCHAR(96) NOT NULL COMMENT '转盘 ID',
		  coin_spent BIGINT NOT NULL COMMENT '消耗金币',
		  rule_version BIGINT NOT NULL COMMENT '规则版本',
		  rtp_window_index BIGINT NOT NULL COMMENT 'RTP 窗口',
		  selected_tier_id VARCHAR(96) NOT NULL COMMENT '命中奖档',
		  reward_type VARCHAR(32) NOT NULL COMMENT 'coin/gift/prop/dress',
		  reward_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '礼物或道具资源 ID',
		  reward_count BIGINT NOT NULL DEFAULT 0 COMMENT '奖励数量',
		  reward_coins BIGINT NOT NULL DEFAULT 0 COMMENT '金币奖励金额',
		  rtp_value_coins BIGINT NOT NULL DEFAULT 0 COMMENT '计入 RTP 的奖励价值',
		  candidate_tiers_json JSON NOT NULL COMMENT '候选过滤快照',
		  rtp_snapshot_json JSON NOT NULL COMMENT 'RTP 快照',
		  prize_metadata_json JSON NOT NULL COMMENT '奖品扩展快照',
		  reward_status VARCHAR(32) NOT NULL COMMENT 'pending/granted/failed',
		  reward_transaction_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '钱包或资产发放交易 ID',
		  reward_failure_reason VARCHAR(255) NOT NULL DEFAULT '' COMMENT '发放失败原因',
		  paid_at_ms BIGINT NOT NULL COMMENT '扣费时间，UTC epoch ms',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
		  PRIMARY KEY (app_code, draw_id),
		  UNIQUE KEY uk_wheel_draw_command (app_code, command_id),
		  KEY idx_wheel_draw_wheel (app_code, wheel_id, created_at_ms),
		  KEY idx_wheel_draw_user (app_code, user_id, created_at_ms),
		  KEY idx_wheel_draw_status (app_code, reward_status, updated_at_ms),
		  KEY idx_wheel_draw_created (app_code, created_at_ms, draw_id)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转盘抽奖事实'`,
		`CREATE TABLE IF NOT EXISTS wheel_draw_stats (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  wheel_id VARCHAR(96) NOT NULL COMMENT '转盘 ID',
		  total_draws BIGINT NOT NULL DEFAULT 0 COMMENT '抽奖次数',
		  unique_users BIGINT NOT NULL DEFAULT 0 COMMENT '参与用户数',
		  total_spent_coins BIGINT NOT NULL DEFAULT 0 COMMENT '消耗金币',
		  total_rtp_value_coins BIGINT NOT NULL DEFAULT 0 COMMENT '计入 RTP 的返奖价值',
		  pending_draws BIGINT NOT NULL DEFAULT 0 COMMENT '待发放数量',
		  granted_draws BIGINT NOT NULL DEFAULT 0 COMMENT '已发放数量',
		  failed_draws 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, wheel_id)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转盘后台汇总统计'`,
		`CREATE TABLE IF NOT EXISTS wheel_draw_stat_users (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  wheel_id VARCHAR(96) NOT NULL COMMENT '转盘 ID',
		  user_id BIGINT NOT NULL COMMENT '用户 ID',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  PRIMARY KEY (app_code, wheel_id, user_id)
		) 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
}
