-- 临时财富/魅力等级只建立 overlay 和副作用事实，不修改 user_growth_level_accounts 的真实经验。
ALTER TABLE growth_level_reward_jobs
  DROP INDEX uk_growth_level_reward_once,
  ADD COLUMN reward_origin VARCHAR(32) NOT NULL DEFAULT 'organic' COMMENT 'organic/temporary' AFTER wallet_grant_id,
  ADD COLUMN temporary_level_id VARCHAR(96) NOT NULL DEFAULT '' AFTER reward_origin,
  ADD COLUMN grant_generation INT NOT NULL DEFAULT 0 AFTER temporary_level_id,
  ADD COLUMN reward_level INT NOT NULL DEFAULT 0 AFTER grant_generation,
  ADD COLUMN is_permanent TINYINT(1) NOT NULL DEFAULT 1 AFTER reward_level,
  ADD COLUMN temporary_expires_at_ms BIGINT NOT NULL DEFAULT 0 AFTER is_permanent,
  ADD COLUMN revoke_status VARCHAR(32) NOT NULL DEFAULT 'none' AFTER temporary_expires_at_ms,
  ADD UNIQUE KEY uk_growth_level_reward_generation
    (app_code, user_id, track, reward_source_type, reward_source_id, reward_origin, grant_generation);

CREATE TABLE IF NOT EXISTS growth_level_reward_wallet_grants (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu', reward_job_id VARCHAR(96) NOT NULL,
  grant_kind VARCHAR(32) NOT NULL, wallet_grant_id VARCHAR(96) NOT NULL,
  revoke_status VARCHAR(32) NOT NULL DEFAULT 'none', revoke_attempt_count INT NOT NULL DEFAULT 0,
  revoke_next_retry_at_ms BIGINT NOT NULL DEFAULT 0, revoke_locked_by VARCHAR(128) NOT NULL DEFAULT '',
  revoke_locked_until_ms BIGINT NOT NULL DEFAULT 0, revoke_failure_reason VARCHAR(255) NOT NULL DEFAULT '',
  revoked_at_ms BIGINT NOT NULL DEFAULT 0, created_at_ms BIGINT NOT NULL, updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, reward_job_id, wallet_grant_id),
  KEY idx_growth_reward_grant_revoke (app_code, revoke_status, revoke_next_retry_at_ms, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS growth_temporary_level_commands (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu', command_id VARCHAR(128) NOT NULL,
  request_hash VARCHAR(64) NOT NULL, user_id BIGINT NOT NULL, operator_admin_id BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL, PRIMARY KEY (app_code, command_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS growth_temporary_levels (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu', temporary_level_id VARCHAR(96) NOT NULL,
  command_id VARCHAR(128) NOT NULL, user_id BIGINT NOT NULL, track VARCHAR(32) NOT NULL,
  target_level INT NOT NULL, target_required_value BIGINT NOT NULL, baseline_total_value BIGINT NOT NULL,
  started_at_ms BIGINT NOT NULL, expires_at_ms BIGINT NOT NULL, status VARCHAR(32) NOT NULL,
  generation INT NOT NULL, operator_admin_id BIGINT NOT NULL, reason VARCHAR(255) NOT NULL DEFAULT '',
  final_level INT NOT NULL DEFAULT 0, activation_notice_status VARCHAR(32) NOT NULL DEFAULT 'pending',
  expiry_notice_status VARCHAR(32) NOT NULL DEFAULT 'skipped', attempt_count INT NOT NULL DEFAULT 0,
  next_retry_at_ms BIGINT NOT NULL DEFAULT 0, locked_by VARCHAR(128) NOT NULL DEFAULT '',
  locked_until_ms BIGINT NOT NULL DEFAULT 0, failure_reason VARCHAR(255) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL, updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, temporary_level_id),
  UNIQUE KEY uk_growth_temp_command_track (app_code, command_id, track),
  KEY idx_growth_temp_user_track (app_code, user_id, track, status, expires_at_ms),
  KEY idx_growth_temp_work (app_code, status, next_retry_at_ms, expires_at_ms, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
