SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 活动 owner 只通过 MQ 传递不可变事实；以下表均由 statistics-service 独占，
-- 在线数据中心不会回查活动业务库或扫描高基数原始事实。
CREATE TABLE IF NOT EXISTS stat_activity_template_summary (
  app_code VARCHAR(32) NOT NULL,
  template_id VARCHAR(96) NOT NULL,
  template_code VARCHAR(64) NOT NULL,
  published_version BIGINT NOT NULL,
  participant_count BIGINT NOT NULL DEFAULT 0,
  visit_event_count BIGINT NOT NULL DEFAULT 0,
  gift_event_count BIGINT NOT NULL DEFAULT 0,
  gift_count BIGINT NOT NULL DEFAULT 0,
  coin_amount BIGINT NOT NULL DEFAULT 0,
  progress_value BIGINT NOT NULL DEFAULT 0,
  completed_count BIGINT NOT NULL DEFAULT 0,
  claimed_count BIGINT NOT NULL DEFAULT 0,
  first_occurred_at_ms BIGINT NOT NULL DEFAULT 0,
  last_occurred_at_ms BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, template_id, published_version),
  KEY idx_stat_activity_template_summary_code (app_code, template_code, published_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动模版版本累计统计';

CREATE TABLE IF NOT EXISTS stat_activity_template_day (
  app_code VARCHAR(32) NOT NULL,
  template_id VARCHAR(96) NOT NULL,
  template_code VARCHAR(64) NOT NULL,
  published_version BIGINT NOT NULL,
  stat_day DATE NOT NULL,
  region_id BIGINT NOT NULL DEFAULT 0,
  participant_count BIGINT NOT NULL DEFAULT 0,
  visit_event_count BIGINT NOT NULL DEFAULT 0,
  gift_event_count BIGINT NOT NULL DEFAULT 0,
  gift_count BIGINT NOT NULL DEFAULT 0,
  coin_amount BIGINT NOT NULL DEFAULT 0,
  progress_value BIGINT NOT NULL DEFAULT 0,
  completed_count BIGINT NOT NULL DEFAULT 0,
  claimed_count BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, template_id, published_version, stat_day, region_id),
  KEY idx_stat_activity_template_day_code (app_code, template_code, published_version, stat_day),
  KEY idx_stat_activity_template_day_region (app_code, template_id, published_version, region_id, stat_day)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动模版版本区域日统计';

CREATE TABLE IF NOT EXISTS stat_activity_template_gift_day (
  app_code VARCHAR(32) NOT NULL,
  template_id VARCHAR(96) NOT NULL,
  template_code VARCHAR(64) NOT NULL,
  published_version BIGINT NOT NULL,
  stat_day DATE NOT NULL,
  region_id BIGINT NOT NULL DEFAULT 0,
  gift_id VARCHAR(96) NOT NULL,
  participant_count BIGINT NOT NULL DEFAULT 0,
  gift_event_count BIGINT NOT NULL DEFAULT 0,
  gift_count BIGINT NOT NULL DEFAULT 0,
  coin_amount BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, template_id, published_version, stat_day, region_id, gift_id),
  KEY idx_stat_activity_template_gift_rank (app_code, template_id, published_version, stat_day, coin_amount, gift_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动模版礼物日聚合';

CREATE TABLE IF NOT EXISTS stat_activity_template_task_day (
  app_code VARCHAR(32) NOT NULL,
  template_id VARCHAR(96) NOT NULL,
  template_code VARCHAR(64) NOT NULL,
  published_version BIGINT NOT NULL,
  stat_day DATE NOT NULL,
  region_id BIGINT NOT NULL DEFAULT 0,
  task_key VARCHAR(96) NOT NULL,
  task_type VARCHAR(64) NOT NULL,
  participant_count BIGINT NOT NULL DEFAULT 0,
  progress_value BIGINT NOT NULL DEFAULT 0,
  completed_count BIGINT NOT NULL DEFAULT 0,
  claimed_count BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, template_id, published_version, stat_day, region_id, task_key),
  KEY idx_stat_activity_template_task_rank (app_code, template_id, published_version, stat_day, completed_count, task_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动模版任务日聚合';

CREATE TABLE IF NOT EXISTS stat_activity_template_participants (
  app_code VARCHAR(32) NOT NULL,
  template_id VARCHAR(96) NOT NULL,
  published_version BIGINT NOT NULL,
  scope_type VARCHAR(20) NOT NULL,
  stat_day DATE NOT NULL,
  region_id BIGINT NOT NULL DEFAULT 0,
  dimension_key VARCHAR(128) NOT NULL DEFAULT '',
  user_id BIGINT NOT NULL,
  first_participated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, template_id, published_version, scope_type, stat_day, region_id, dimension_key, user_id),
  KEY idx_stat_activity_participant_dimension (app_code, template_id, published_version, scope_type, dimension_key, stat_day, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动模版参与者去重投影';
