CREATE DATABASE IF NOT EXISTS doc_forge CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE doc_forge;

CREATE TABLE IF NOT EXISTS templates (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL COMMENT '模板名称',
    description TEXT DEFAULT '' COMMENT '描述',
    file_path VARCHAR(500) NOT NULL COMMENT 'MinIO 对象路径',
    paragraph_count INT DEFAULT 0 COMMENT '段落数',
    status VARCHAR(20) DEFAULT 'draft' COMMENT 'draft/ready',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS ai_models (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL COMMENT '模型名称',
    provider VARCHAR(100) DEFAULT '' COMMENT '供应厂商',
    api_format VARCHAR(20) DEFAULT 'openai' COMMENT 'anthropic/openai',
    api_endpoint VARCHAR(500) DEFAULT '' COMMENT 'API 地址',
    api_key_encrypted TEXT DEFAULT '' COMMENT '加密后的 API Key',
    supports_streaming TINYINT(1) DEFAULT 0 COMMENT '是否支持流式传输',
    enable_reasoning TINYINT(1) DEFAULT 0 COMMENT '是否开启思考模式',
    status VARCHAR(20) DEFAULT 'enabled' COMMENT 'enabled/disabled',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS paragraphs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    template_id INT NOT NULL,
    sort_index INT DEFAULT 0 COMMENT '排序',
    anchor_title VARCHAR(500) DEFAULT '' COMMENT '原始标题锚点',
    title VARCHAR(500) DEFAULT '' COMMENT '段落标题',
    content TEXT DEFAULT '' COMMENT '正文内容',
    style_json TEXT DEFAULT '{}' COMMENT '样式 JSON',
    is_table TINYINT(1) DEFAULT 0 COMMENT '是否为表格',
    table_json TEXT DEFAULT '{}' COMMENT '表格结构 JSON',
    edit_mode VARCHAR(20) DEFAULT 'manual' COMMENT 'manual/ai',
    write_mode VARCHAR(30) DEFAULT 'replace_section' COMMENT 'replace_section/append_after_heading/replace_heading_only',
    model_id INT DEFAULT NULL COMMENT '指定模型',
    need_prompt TINYINT(1) DEFAULT 1 COMMENT '是否需要提示词',
    prompt_text TEXT DEFAULT '' COMMENT '预设提示词',
    need_file TINYINT(1) DEFAULT 0 COMMENT '是否需要参考文件',
    file_note TEXT DEFAULT '' COMMENT '备注说明',
    output_format VARCHAR(20) DEFAULT 'text' COMMENT 'text/table/mixed/chart',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (template_id) REFERENCES templates(id) ON DELETE CASCADE,
    FOREIGN KEY (model_id) REFERENCES ai_models(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS template_blocks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    template_id INT NOT NULL,
    source_paragraph_id INT DEFAULT NULL COMMENT '来源段落 ID',
    parent_block_id INT DEFAULT NULL COMMENT '父块 ID',
    sort_index INT DEFAULT 0 COMMENT '排序',
    block_type VARCHAR(30) DEFAULT 'text' COMMENT 'heading/text/table/ai_slot/variable',
    anchor_ref VARCHAR(500) DEFAULT '' COMMENT '原始锚点引用',
    title VARCHAR(500) DEFAULT '' COMMENT '块标题',
    content_json TEXT DEFAULT '{}' COMMENT '块内容 JSON',
    style_json TEXT DEFAULT '{}' COMMENT '块样式 JSON',
    edit_mode VARCHAR(20) DEFAULT 'manual' COMMENT 'manual/ai',
    placeholder_key VARCHAR(120) DEFAULT '' COMMENT 'AI 占位键',
    variable_key VARCHAR(120) DEFAULT '' COMMENT '变量键',
    default_value TEXT DEFAULT '' COMMENT '默认值',
    model_id INT DEFAULT NULL COMMENT '指定模型',
    need_prompt TINYINT(1) DEFAULT 1 COMMENT '是否需要提示词',
    prompt_text TEXT DEFAULT '' COMMENT '预设提示词',
    need_file TINYINT(1) DEFAULT 0 COMMENT '是否需要参考文件',
    file_note TEXT DEFAULT '' COMMENT '备注说明',
    output_format VARCHAR(20) DEFAULT 'text' COMMENT 'text/table/mixed/chart',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (template_id) REFERENCES templates(id) ON DELETE CASCADE,
    FOREIGN KEY (source_paragraph_id) REFERENCES paragraphs(id) ON DELETE SET NULL,
    FOREIGN KEY (parent_block_id) REFERENCES template_blocks(id) ON DELETE SET NULL,
    FOREIGN KEY (model_id) REFERENCES ai_models(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS documents (
    id INT AUTO_INCREMENT PRIMARY KEY,
    template_id INT NOT NULL,
    name VARCHAR(255) DEFAULT '' COMMENT '文档名称',
    para_count_done INT DEFAULT 0 COMMENT '已完成段落数',
    para_count_total INT DEFAULT 0 COMMENT '总段落数',
    status VARCHAR(20) DEFAULT 'pending' COMMENT 'pending/generating/completed/failed/cancelled',
    file_path VARCHAR(500) DEFAULT '' COMMENT 'MinIO 对象路径',
    error TEXT DEFAULT '' COMMENT '错误信息',
    request_payload_json TEXT COMMENT '提交任务时的文件与段落配置快照',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (template_id) REFERENCES templates(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS generation_logs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    document_id INT NOT NULL,
    paragraph_id INT NOT NULL,
    model_id INT DEFAULT NULL,
    status VARCHAR(20) DEFAULT 'pending' COMMENT 'pending/generating/success/failed',
    content TEXT DEFAULT '' COMMENT '生成的内容',
    duration FLOAT DEFAULT 0 COMMENT '耗时秒数',
    error_msg TEXT DEFAULT '' COMMENT '错误信息',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE,
    FOREIGN KEY (paragraph_id) REFERENCES paragraphs(id) ON DELETE CASCADE,
    FOREIGN KEY (model_id) REFERENCES ai_models(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS reference_files (
    id INT AUTO_INCREMENT PRIMARY KEY,
    file_name VARCHAR(255) DEFAULT '' COMMENT '原始文件名',
    file_path VARCHAR(500) DEFAULT '' COMMENT 'MinIO 对象路径',
    file_size INT DEFAULT 0 COMMENT '文件大小',
    content_type VARCHAR(120) DEFAULT '' COMMENT '文件类型',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
