-- 用户表
CREATE TABLE IF NOT EXISTS user (
                                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                                    username TEXT NOT NULL UNIQUE,
                                    password TEXT NOT NULL,
                                    email TEXT,
                                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Markdown文件表
CREATE TABLE IF NOT EXISTS markdown_file (
                                             id INTEGER PRIMARY KEY AUTOINCREMENT,
                                             user_id INTEGER NOT NULL,
                                             title TEXT NOT NULL,
                                             file_name TEXT NOT NULL,
                                             content TEXT NOT NULL,
                                             created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                                             updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                                             FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE
    );

-- 图片表
CREATE TABLE IF NOT EXISTS image (
                                     id INTEGER PRIMARY KEY AUTOINCREMENT,
                                     user_id INTEGER NOT NULL,
                                     markdown_id INTEGER,
                                     original_name TEXT NOT NULL,
                                     stored_name TEXT NOT NULL UNIQUE,
                                     url TEXT NOT NULL,
                                     size INTEGER NOT NULL,
                                     content_type TEXT NOT NULL,
                                     created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                                     FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE,
    FOREIGN KEY (markdown_id) REFERENCES markdown_file(id) ON DELETE SET NULL
    );