SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE DATABASE IF NOT EXISTS hyapp_statistics DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hyapp_statistics;

CREATE TABLE IF NOT EXISTS statistics_event_consumption (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  source VARCHAR(32) NOT NULL COMMENT '事件来源 owner service',
  event_id VARCHAR(160) NOT NULL COMMENT '事件幂等 ID',
  event_type VARCHAR(64) NOT NULL COMMENT '事件类型',
  status VARCHAR(32) NOT NULL COMMENT '消费状态',
  consumed_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, source, event_id),
  KEY idx_statistics_event_type (app_code, source, event_type, consumed_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='统计事件消费幂等表';

CREATE TABLE IF NOT EXISTS stat_app_day_country (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  stat_day DATE NOT NULL COMMENT 'UTC 统计日',
  country_id BIGINT NOT NULL DEFAULT 0 COMMENT '国家或区域维度 ID，0 表示未知或全局',
  new_users BIGINT NOT NULL DEFAULT 0 COMMENT '新增用户数',
  active_users BIGINT NOT NULL DEFAULT 0 COMMENT '当日活跃去重用户数',
  paid_users BIGINT NOT NULL DEFAULT 0 COMMENT '当日充值去重用户数',
  recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '总充值美元最小单位',
  new_user_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '新增充值美元最小单位',
  coin_seller_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '币商充值美元最小单位',
  mifapay_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT 'MifaPay 充值美元最小单位',
  google_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT 'Google 充值美元最小单位',
  gift_coin_spent BIGINT NOT NULL DEFAULT 0 COMMENT '礼物消耗金币',
  lucky_gift_turnover BIGINT NOT NULL DEFAULT 0 COMMENT '幸运礼物流水',
  lucky_gift_payers BIGINT NOT NULL DEFAULT 0 COMMENT '幸运礼物付费去重人数',
  game_turnover BIGINT NOT NULL DEFAULT 0 COMMENT '游戏流水',
  game_players BIGINT NOT NULL DEFAULT 0 COMMENT '游戏参与去重人数',
  game_payout BIGINT NOT NULL DEFAULT 0 COMMENT '游戏返奖',
  game_refund BIGINT NOT NULL DEFAULT 0 COMMENT '游戏退款',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, stat_day, country_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='App 国家日统计聚合表';

CREATE TABLE IF NOT EXISTS stat_user_day_activity (
  app_code VARCHAR(32) NOT NULL,
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  user_id BIGINT NOT NULL,
  first_active_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_day, user_id),
  KEY idx_stat_user_day_country (app_code, stat_day, country_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户日活跃去重表';

CREATE TABLE IF NOT EXISTS stat_user_registration (
  app_code VARCHAR(32) NOT NULL,
  user_id BIGINT NOT NULL,
  registered_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  registered_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id),
  KEY idx_stat_user_registration_day (app_code, registered_day, country_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户注册日留存 cohort 表';

CREATE TABLE IF NOT EXISTS stat_recharge_day_payers (
  app_code VARCHAR(32) NOT NULL,
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  user_id BIGINT NOT NULL,
  first_paid_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_day, user_id),
  KEY idx_stat_recharge_payer_country (app_code, stat_day, country_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='充值日付费用户去重表';

CREATE TABLE IF NOT EXISTS stat_lucky_gift_day_payers (
  app_code VARCHAR(32) NOT NULL,
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  user_id BIGINT NOT NULL,
  first_paid_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_day, user_id),
  KEY idx_stat_lucky_payer_country (app_code, stat_day, country_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物日付费去重表';

CREATE TABLE IF NOT EXISTS stat_game_day_country (
  app_code VARCHAR(32) NOT NULL,
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  platform_code VARCHAR(64) NOT NULL DEFAULT '',
  game_id VARCHAR(96) NOT NULL,
  turnover_coin BIGINT NOT NULL DEFAULT 0,
  payout_coin BIGINT NOT NULL DEFAULT 0,
  refund_coin BIGINT NOT NULL DEFAULT 0,
  player_count BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_day, country_id, platform_code, game_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏国家日统计聚合表';

CREATE TABLE IF NOT EXISTS stat_game_day_players (
  app_code VARCHAR(32) NOT NULL,
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  platform_code VARCHAR(64) NOT NULL DEFAULT '',
  game_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  first_played_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_day, country_id, platform_code, game_id, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏日参与用户去重表';
