-- SimpleChat 数据库初始化脚本
-- 创建数据库: chat.db

-- 用户表
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(100) NOT NULL,
    nickname VARCHAR(50),
    avatar VARCHAR(200) DEFAULT 'default.png',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 好友关系表
CREATE TABLE IF NOT EXISTS friendships (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    friend_id INTEGER NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id),
    FOREIGN KEY (friend_id) REFERENCES users(id),
    UNIQUE(user_id, friend_id)
);

-- 消息表
CREATE TABLE IF NOT EXISTS messages (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    sender_id INTEGER NOT NULL,
    receiver_id INTEGER,
    group_id INTEGER,
    content TEXT NOT NULL,
    msg_type VARCHAR(20) DEFAULT 'text',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (sender_id) REFERENCES users(id)
);

-- 群组表
CREATE TABLE IF NOT EXISTS groups (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name VARCHAR(100) NOT NULL,
    creator_id INTEGER NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (creator_id) REFERENCES users(id)
);

-- 群组成员表
CREATE TABLE IF NOT EXISTS group_members (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    group_id INTEGER NOT NULL,
    user_id INTEGER NOT NULL,
    join_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (group_id) REFERENCES groups(id),
    FOREIGN KEY (user_id) REFERENCES users(id),
    UNIQUE(group_id, user_id)
);

-- 创建默认管理员账户 (密码: 123456)
INSERT OR IGNORE INTO users (username, password, nickname) 
VALUES ('admin', 'e10adc3949ba59abbe56e057f20f883e', '系统管理员');

-- 创建测试用户
INSERT OR IGNORE INTO users (username, password, nickname) 
VALUES 
    ('user1', 'e10adc3949ba59abbe56e057f20f883e', '用户一'),
    ('user2', 'e10adc3949ba59abbe56e057f20f883e', '用户二'),
    ('user3', 'e10adc3949ba59abbe56e057f20f883e', '用户三');

-- 创建测试好友关系
INSERT OR IGNORE INTO friendships (user_id, friend_id)
SELECT u1.id, u2.id
FROM users u1, users u2
WHERE u1.username = 'admin' AND u2.username = 'user1'
   OR u1.username = 'admin' AND u2.username = 'user2'
   OR u1.username = 'user1' AND u2.username = 'user2';

-- 创建测试群组
INSERT OR IGNORE INTO groups (name, creator_id)
SELECT '测试群组', id FROM users WHERE username = 'admin';

-- 添加群组成员
INSERT OR IGNORE INTO group_members (group_id, user_id)
SELECT g.id, u.id
FROM groups g, users u
WHERE g.name = '测试群组' 
  AND u.username IN ('admin', 'user1', 'user2', 'user3');

-- 创建测试消息
INSERT OR IGNORE INTO messages (sender_id, receiver_id, content)
SELECT u1.id, u2.id, '你好，这是测试消息！'
FROM users u1, users u2
WHERE u1.username = 'admin' AND u2.username = 'user1';

-- 创建索引以提高查询性能
CREATE INDEX IF NOT EXISTS idx_messages_sender ON messages(sender_id);
CREATE INDEX IF NOT EXISTS idx_messages_receiver ON messages(receiver_id);
CREATE INDEX IF NOT EXISTS idx_messages_group ON messages(group_id);
CREATE INDEX IF NOT EXISTS idx_friendships_user ON friendships(user_id);
CREATE INDEX IF NOT EXISTS idx_friendships_friend ON friendships(friend_id);
CREATE INDEX IF NOT EXISTS idx_group_members_group ON group_members(group_id);
CREATE INDEX IF NOT EXISTS idx_group_members_user ON group_members(user_id);