-- Yumi Game King: real winning-income ranking, separate reward configs, and daily chance cap.
--
-- Performance review:
-- 1. Activity/rank-reward/settlement are small configuration/result tables; their ALTERs are
--    bounded metadata/index rebuilds and do not touch the 35M-row task archive.
-- 2. User aggregate ALTERs add two narrow columns plus ranking indexes. Run during low traffic:
--    MySQL builds each index once; online DDL keeps reads/writes available with LOCK=NONE.
-- 3. No historical ledger is scanned or backfilled here. New winning rows arrive from the
--    canonical GAME_WIN_GOLD event. The daily-user index makes chance sums proportional to one
--    user's active dates instead of all activity participants.
-- 4. Existing six TYCOON reward rows are copied once to VICTORIOUS so old activities remain
--    editable; operators can replace each board's six resource groups before enabling.

ALTER TABLE `yumi_game_king_activity`
  ADD COLUMN `daily_chance_limit` INT NOT NULL DEFAULT 10 AFTER `coin_per_draw`,
  ALGORITHM=INSTANT;

ALTER TABLE `yumi_game_king_rank_reward`
  DROP INDEX `uk_yumi_gk_rank_range`,
  DROP INDEX `idx_yumi_gk_rank_start`,
  ADD COLUMN `ranking_type` VARCHAR(32) NOT NULL DEFAULT 'TYCOON' AFTER `activity_id`,
  ADD UNIQUE KEY `uk_yumi_gk_rank_range` (`activity_id`, `ranking_type`, `start_rank`, `end_rank`),
  ADD KEY `idx_yumi_gk_rank_start` (`activity_id`, `ranking_type`, `start_rank`),
  ALGORITHM=INPLACE, LOCK=NONE;

INSERT INTO `yumi_game_king_rank_reward` (
  `id`, `activity_id`, `ranking_type`, `start_rank`, `end_rank`, `resource_group_id`,
  `reward_name`, `reward_items_json`, `create_time`, `update_time`
)
SELECT UUID_SHORT(), `activity_id`, 'VICTORIOUS', `start_rank`, `end_rank`, `resource_group_id`,
       `reward_name`, `reward_items_json`, `create_time`, `update_time`
FROM `yumi_game_king_rank_reward`
WHERE `ranking_type` = 'TYCOON';

ALTER TABLE `yumi_game_king_consume_ledger`
  ADD COLUMN `event_type` VARCHAR(32) NOT NULL DEFAULT 'GAME_CONSUME_GOLD' AFTER `sys_origin`,
  ALGORITHM=INSTANT;

ALTER TABLE `yumi_game_king_user`
  ADD COLUMN `total_won` BIGINT NOT NULL DEFAULT 0 AFTER `total_consumed`,
  ADD COLUMN `win_reached_time` DATETIME(3) NULL AFTER `consume_reached_time`,
  ADD KEY `idx_yumi_gk_user_win_rank` (
    `activity_id`, `total_won` DESC, `win_reached_time` ASC, `user_id` ASC
  ),
  ALGORITHM=INPLACE, LOCK=NONE;

ALTER TABLE `yumi_game_king_user_daily`
  ADD COLUMN `total_won` BIGINT NOT NULL DEFAULT 0 AFTER `total_consumed`,
  ADD COLUMN `win_reached_time` DATETIME(3) NULL AFTER `consume_reached_time`,
  ADD KEY `idx_yumi_gk_daily_user_chance` (`activity_id`, `user_id`, `stat_date`),
  ADD KEY `idx_yumi_gk_daily_win_rank` (
    `activity_id`, `stat_date`, `total_won` DESC, `win_reached_time` ASC, `user_id` ASC
  ),
  ALGORITHM=INPLACE, LOCK=NONE;

ALTER TABLE `yumi_game_king_settlement_record`
  DROP INDEX `uk_yumi_gk_settle_user`,
  DROP INDEX `uk_yumi_gk_settle_rank`,
  ADD COLUMN `ranking_type` VARCHAR(32) NOT NULL DEFAULT 'TYCOON' AFTER `activity_id`,
  ADD COLUMN `score` BIGINT NOT NULL DEFAULT 0 AFTER `total_consumed`,
  ADD UNIQUE KEY `uk_yumi_gk_settle_user` (`activity_id`, `ranking_type`, `user_id`),
  ADD UNIQUE KEY `uk_yumi_gk_settle_rank` (`activity_id`, `ranking_type`, `rank_no`),
  ALGORITHM=INPLACE, LOCK=NONE;

UPDATE `yumi_game_king_settlement_record`
SET `score` = `total_consumed`
WHERE `ranking_type` = 'TYCOON' AND `score` = 0;
