-- ─────────────────────────────────────────────────────────────────────────────
-- V1: Initial schema — mirrors the existing schema.sql / data.sql.
-- Managed by Flyway; do NOT modify after first deployment.
-- Subsequent schema changes belong in V2, V3, ...
-- ─────────────────────────────────────────────────────────────────────────────

-- User table
CREATE TABLE IF NOT EXISTS user
(
    id                INTEGER      NOT NULL PRIMARY KEY AUTO_INCREMENT,
    username          VARCHAR(50),
    realname          VARCHAR(50),
    email             VARCHAR(100),
    password          VARCHAR(255), -- must hold BCrypt hashes (>=68 chars); V2 migrates existing data
    join_time         DATETIME,
    age               INTEGER,
    avatar            TEXT,
    gender            CHAR(10),
    phone             VARCHAR(20),
    discussions_count INTEGER,
    comments_count    INTEGER,
    last_seen_time    DATETIME,
    flag              INTEGER,
    level             INTEGER
);

-- Discussion (thread) table
CREATE TABLE IF NOT EXISTS discussion
(
    id                 INTEGER      NOT NULL PRIMARY KEY AUTO_INCREMENT,
    title              VARCHAR(200),
    content            TEXT,
    comments_count     INTEGER,
    participants_count INTEGER,
    number_index       INTEGER,
    start_time         DATETIME,
    start_user_id      INTEGER,
    start_post_id      INTEGER,
    last_time          DATETIME,
    last_user_id       INTEGER,
    last_post_id       INTEGER,
    last_post_number   INTEGER,
    is_approved        INTEGER,
    like_count         INTEGER,
    ip_address         VARCHAR(200),
    create_id          INTEGER,
    create_time        DATETIME
);

-- Discussion ↔ Tag association table
CREATE TABLE IF NOT EXISTS discussiontag
(
    id            INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    discussion_id INTEGER NOT NULL,
    tag_id        INTEGER
);

-- Post (reply/comment) table
CREATE TABLE IF NOT EXISTS post
(
    id            INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    discussion_id INTEGER,
    number        INTEGER,
    time          DATETIME,
    user_id       INTEGER,
    type          VARCHAR(20),
    content       TEXT,
    edit_time     DATETIME,
    edit_user_id  INTEGER,
    parent_id     INTEGER,
    ip_address    VARCHAR(200),
    copyright     VARCHAR(200),
    is_approved   INTEGER,
    create_id     INTEGER,
    create_time   DATETIME
);

-- Settings table
CREATE TABLE IF NOT EXISTS setting
(
    id            INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    setting_key   VARCHAR(50),
    setting_value TEXT
);

-- Tag table
CREATE TABLE IF NOT EXISTS tag
(
    id                 INTEGER      NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name               VARCHAR(200),
    description        VARCHAR(200),
    color              VARCHAR(200),
    icon               VARCHAR(200),
    position           INTEGER,
    parent_id          INTEGER,
    discussions_count  TEXT,
    last_time          DATETIME,
    last_discussion_id INTEGER,
    create_id          INTEGER,
    create_time        DATETIME
);

-- Like / Collect table
CREATE TABLE IF NOT EXISTS likecollect
(
    id              INTEGER      NOT NULL PRIMARY KEY AUTO_INCREMENT,
    discussion_id   INTEGER,
    discussion_name VARCHAR(200),
    tag_id          INTEGER,
    post_id         INTEGER,
    post_content    TEXT,
    user_id         INTEGER,
    user_name       VARCHAR(200),
    type            VARCHAR(20),
    like_type       VARCHAR(20),
    collect_type    VARCHAR(20),
    create_id       INTEGER,
    create_time     DATETIME
);

-- ─── Seed data (dev/test) ────────────────────────────────────────────────────
-- Passwords below are BCrypt hashes of '123456'.
-- Generated with: BCrypt.encode("123456")
-- DO NOT use these credentials in production.
INSERT INTO user (id, username, realname, email, password, join_time, age, avatar, gender, phone,
                  discussions_count, comments_count, last_seen_time, flag, level)
VALUES (1, 'admin', '管理员', 'admin@jiscuss.local',
        '$2a$10$7EqJtq98hPqEX7fNZaFWoOziYXMEIUBm..wTkPE3bsUJn4Lm7oHVC',
        '2019-09-09 00:00:00', 31, '', '男', '13800000001', 0, 0, '2019-09-09 00:00:00', 1, 1);

INSERT INTO user (id, username, realname, email, password, join_time, age, avatar, gender, phone,
                  discussions_count, comments_count, last_seen_time, flag, level)
VALUES (2, 'test', '测试用户', 'test@jiscuss.local',
        '$2a$10$7EqJtq98hPqEX7fNZaFWoOziYXMEIUBm..wTkPE3bsUJn4Lm7oHVC',
        '2019-09-09 00:00:00', 31, '', '男', '13800000002', 0, 0, '2019-09-09 00:00:00', 1, 0);

INSERT INTO discussion (id, title, content, start_time, start_user_id, last_time, last_user_id, create_id, create_time)
VALUES (1, '测试主题1', '测试内容1', '2020-09-19 00:00:00', 1, '2020-09-29 00:00:00', 2, 1, '2020-09-09 00:00:00');

INSERT INTO tag (id, name, icon, position)
VALUES (1, '测试标签1', 'edit', 1);

INSERT INTO post (id, discussion_id, number, time, user_id, content, create_id, create_time)
VALUES (1, 1, 1, '2020-02-09 00:00:00', 1, '评论内容222', 1, '2020-08-09 00:00:00');
