/* Replace with your SQL commands */
CREATE TABLE discussion_posts (
    id INT PRIMARY KEY AUTO_INCREMENT,
    type VARCHAR(50) NOT NULL,
    poster_id INT NOT NULL,
    title VARCHAR(100) NOT NULL,
    content JSON NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    is_deleted BOOLEAN DEFAULT FALSE,
    is_locked BOOLEAN DEFAULT FALSE,
    is_open BOOLEAN GENERATED ALWAYS AS (
        NOT is_deleted
        AND NOT is_locked
    ) STORED,
    FOREIGN KEY (poster_id) REFERENCES members(id) ON DELETE CASCADE
);
CREATE TABLE discussion_comments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    post_id INT NOT NULL,
    poster_id INT NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    is_deleted BOOLEAN DEFAULT FALSE,
    FOREIGN KEY (post_id) REFERENCES discussion_posts(id) ON DELETE CASCADE,
    FOREIGN KEY (poster_id) REFERENCES members(id) ON DELETE CASCADE
);
CREATE INDEX idx_discussion_posts_title ON discussion_posts(title);
CREATE INDEX idx_discussion_posts_type ON discussion_posts(type);
CREATE INDEX idx_discussion_posts_poster_id ON discussion_posts(poster_id);
CREATE INDEX idx_discussion_comments_post_id ON discussion_comments(post_id);
CREATE INDEX idx_discussion_comments_poster_id ON discussion_comments(poster_id);
CREATE INDEX idx_discussion_posts_is_open ON discussion_posts(is_open);