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

USE abtest;

-- 实验表
CREATE TABLE IF NOT EXISTS experiments (
    id          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name        VARCHAR(128)  NOT NULL COMMENT '实验名称',
    description VARCHAR(512)  NOT NULL DEFAULT '' COMMENT '实验描述',
    status      TINYINT       NOT NULL DEFAULT 0 COMMENT '状态: 0-草稿 1-运行中 2-已暂停 3-已结束',
    start_time  DATETIME      NULL COMMENT '开始时间',
    end_time    DATETIME      NULL COMMENT '结束时间',
    created_at  DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at  DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AB测试实验表';

-- 实验变体表
CREATE TABLE IF NOT EXISTS variants (
    id            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    experiment_id BIGINT UNSIGNED NOT NULL COMMENT '所属实验ID',
    name          VARCHAR(128)    NOT NULL COMMENT '变体名称 (如 control / treatment)',
    description   VARCHAR(512)    NOT NULL DEFAULT '' COMMENT '变体描述',
    weight        INT UNSIGNED    NOT NULL DEFAULT 1 COMMENT '流量权重',
    params        JSON            NULL COMMENT '变体参数(JSON)',
    created_at    DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at    DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    KEY idx_experiment_id (experiment_id),
    UNIQUE KEY uk_exp_name (experiment_id, name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='实验变体表';

-- 用户分组分配表
CREATE TABLE IF NOT EXISTS user_assignments (
    id            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    experiment_id BIGINT UNSIGNED NOT NULL COMMENT '实验ID',
    user_id       VARCHAR(128)    NOT NULL COMMENT '用户标识',
    variant_id    BIGINT UNSIGNED NOT NULL COMMENT '分配到的变体ID',
    assigned_at   DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uk_exp_user (experiment_id, user_id),
    KEY idx_variant_id (variant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户分组分配表';

-- 白名单表（指定用户强制进入某变体）
CREATE TABLE IF NOT EXISTS whitelist (
    id            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    experiment_id BIGINT UNSIGNED NOT NULL COMMENT '实验ID',
    user_id       VARCHAR(128)    NOT NULL COMMENT '用户标识',
    variant_id    BIGINT UNSIGNED NOT NULL COMMENT '强制分配的变体ID',
    created_at    DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uk_exp_user (experiment_id, user_id),
    KEY idx_variant_id (variant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='白名单表';

-- 事件追踪表
CREATE TABLE IF NOT EXISTS events (
    id            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    experiment_id BIGINT UNSIGNED NOT NULL COMMENT '实验ID',
    variant_id    BIGINT UNSIGNED NOT NULL COMMENT '变体ID',
    user_id       VARCHAR(128)    NOT NULL COMMENT '用户标识',
    event_type    VARCHAR(64)     NOT NULL COMMENT '事件类型 (如 exposure / conversion)',
    event_value   DOUBLE          NOT NULL DEFAULT 0 COMMENT '事件数值',
    metadata      JSON            NULL COMMENT '附加数据',
    created_at    DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_exp_variant (experiment_id, variant_id),
    KEY idx_user (user_id),
    KEY idx_event_type (event_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='事件追踪表';
