-- 创建数据库
CREATE DATABASE message_archive CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE message_archive;

-- 创建群聊表
CREATE TABLE t_encyclopedia_groups (
    group_id VARCHAR(50) PRIMARY KEY,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB CHARACTER SET utf8mb4;

-- 创建玩家表
CREATE TABLE t_encyclopedia_players (
    player_id VARCHAR(50) NOT NULL,
    group_id VARCHAR(50) NOT NULL,
    player_name VARCHAR(50) NOT NULL,
    points INT DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (player_id, group_id)
) ENGINE=InnoDB CHARACTER SET utf8mb4;

-- 创建活跃任务表
CREATE TABLE t_encyclopedia_active_tasks (
    active_task_id INT AUTO_INCREMENT PRIMARY KEY,
    group_id VARCHAR(50) NOT NULL,
    question VARCHAR(255) NOT NULL,
    answer VARCHAR(100) NOT NULL,
    score INT NOT NULL,
    description TEXT,
    holder_id VARCHAR(50) NOT NULL,
    assigned_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    status ENUM('pending', 'completed') DEFAULT 'pending',
    INDEX (group_id)
) ENGINE=InnoDB CHARACTER SET utf8mb4;

-- 创建任务历史表
CREATE TABLE t_encyclopedia_task_history (
    history_id INT AUTO_INCREMENT PRIMARY KEY,
    group_id VARCHAR(50) NOT NULL,
    active_task_id INT NOT NULL,
    player_id VARCHAR(50) NOT NULL,
    answer VARCHAR(100) NOT NULL,
    is_correct BOOLEAN DEFAULT FALSE,
    points_earned INT DEFAULT 0,
    completed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX (group_id)
) ENGINE=InnoDB CHARACTER SET utf8mb4;

-- 初始化数据
-- 添加群聊
INSERT INTO t_encyclopedia_groups (group_id) VALUES
('group1'),
('group2');

-- 添加玩家
INSERT INTO t_encyclopedia_players (player_id, group_id, player_name) VALUES
('player1001', 'group1', '玩家1'),
('player1002', 'group1', '玩家2'),
('player2001', 'group2', '玩家A'),
('player2002', 'group2', '玩家B');