package mysql

import "context"

func (r *Repository) ensureWeeklyStarTables(ctx context.Context) error {
	statements := []string{
		`CREATE TABLE IF NOT EXISTS weekly_star_cycles (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  cycle_id VARCHAR(96) NOT NULL COMMENT '周星周期 ID',
		  activity_code VARCHAR(64) NOT NULL DEFAULT 'weekly_star' COMMENT '活动编码',
		  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '区域 ID，0 表示默认配置',
		  title VARCHAR(128) NOT NULL DEFAULT '' COMMENT '周期标题',
		  status VARCHAR(32) NOT NULL DEFAULT 'draft' COMMENT '状态',
		  start_ms BIGINT NOT NULL COMMENT 'UTC epoch ms 生效开始，包含',
		  end_ms BIGINT NOT NULL COMMENT 'UTC epoch ms 生效结束，不包含',
		  created_by_admin_id BIGINT NOT NULL DEFAULT 0 COMMENT '创建管理员',
		  updated_by_admin_id BIGINT NOT NULL DEFAULT 0 COMMENT '更新管理员',
		  settled_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '结算完成时间',
		  locked_by VARCHAR(96) NOT NULL DEFAULT '' COMMENT '结算 worker 锁',
		  lock_until_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, cycle_id),
		  KEY idx_weekly_star_cycle_current (app_code, activity_code, status, region_id, start_ms, end_ms),
		  KEY idx_weekly_star_cycle_due (app_code, activity_code, status, end_ms, lock_until_ms)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='周星指定礼物积分周期'`,
		`CREATE TABLE IF NOT EXISTS weekly_star_cycle_gifts (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  cycle_id VARCHAR(96) NOT NULL COMMENT '周星周期 ID',
		  gift_id VARCHAR(96) NOT NULL COMMENT '参与计分礼物 ID',
		  sort_order INT NOT NULL DEFAULT 0 COMMENT '展示顺序',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  PRIMARY KEY (app_code, cycle_id, gift_id),
		  KEY idx_weekly_star_gift_match (app_code, gift_id, cycle_id)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='周星周期指定礼物'`,
		`CREATE TABLE IF NOT EXISTS weekly_star_cycle_rewards (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  cycle_id VARCHAR(96) NOT NULL COMMENT '周星周期 ID',
		  rank_no INT NOT NULL COMMENT '排名，1/2/3',
		  resource_group_id BIGINT NOT NULL COMMENT '奖励资源组 ID',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
		  PRIMARY KEY (app_code, cycle_id, rank_no)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='周星周期 Top 奖励'`,
		`CREATE TABLE IF NOT EXISTS weekly_star_user_scores (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  cycle_id VARCHAR(96) NOT NULL COMMENT '周星周期 ID',
		  user_id BIGINT NOT NULL COMMENT '用户 ID',
		  score BIGINT NOT NULL DEFAULT 0 COMMENT '累计积分，等于活动礼物消耗金币',
		  first_scored_at_ms BIGINT NOT NULL COMMENT '首次得分时间',
		  last_scored_at_ms BIGINT NOT NULL 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, cycle_id, user_id),
		  KEY idx_weekly_star_score_rank (app_code, cycle_id, score, first_scored_at_ms, user_id)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='周星用户积分榜'`,
		`CREATE TABLE IF NOT EXISTS weekly_star_score_events (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  source_event_id VARCHAR(128) NOT NULL COMMENT 'room-service outbox event_id',
		  cycle_id VARCHAR(96) NOT NULL COMMENT '周星周期 ID',
		  user_id BIGINT NOT NULL COMMENT '送礼用户 ID',
		  gift_id VARCHAR(96) NOT NULL COMMENT '礼物 ID',
		  score_delta BIGINT NOT NULL COMMENT '本次增加积分',
		  occurred_at_ms BIGINT NOT NULL COMMENT '事件发生时间，UTC epoch ms',
		  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
		  PRIMARY KEY (app_code, source_event_id),
		  KEY idx_weekly_star_score_event_cycle (app_code, cycle_id, user_id, occurred_at_ms)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='周星送礼积分事件幂等表'`,
		`CREATE TABLE IF NOT EXISTS weekly_star_settlements (
		  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
		  settlement_id VARCHAR(96) NOT NULL COMMENT '周星结算 ID',
		  cycle_id VARCHAR(96) NOT NULL COMMENT '周星周期 ID',
		  rank_no INT NOT NULL COMMENT '排名，1/2/3',
		  user_id BIGINT NOT NULL COMMENT '获奖用户 ID',
		  score BIGINT NOT NULL DEFAULT 0 COMMENT '结算积分',
		  resource_group_id BIGINT NOT NULL COMMENT '奖励资源组 ID',
		  wallet_command_id VARCHAR(128) NOT NULL COMMENT 'wallet 幂等命令',
		  wallet_grant_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'wallet grant ID',
		  status VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT '发放状态',
		  failure_reason VARCHAR(512) NOT NULL DEFAULT '' COMMENT '失败原因',
		  attempt_count 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 (app_code, settlement_id),
		  UNIQUE KEY uk_weekly_star_settlement_rank (app_code, cycle_id, rank_no),
		  UNIQUE KEY uk_weekly_star_wallet_command (app_code, wallet_command_id),
		  KEY idx_weekly_star_settlement_cycle (app_code, cycle_id, status)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='周星 Top 奖励结算'`,
	}
	for _, statement := range statements {
		if _, err := r.db.ExecContext(ctx, statement); err != nil {
			return err
		}
	}
	return nil
}
