CREATE TABLE IF NOT EXISTS knowledge_base (
    id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID',
    name VARCHAR(255) NOT NULL COMMENT '知识库名称',
    description TEXT COMMENT '知识库描述',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    create_by BIGINT COMMENT '创建人ID'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识库';

CREATE TABLE IF NOT EXISTS knowledge_document (
    id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID',
    kb_id BIGINT NOT NULL COMMENT '知识库ID',
    original_name VARCHAR(512) NOT NULL COMMENT '原始文件名',
    stored_name VARCHAR(512) NOT NULL COMMENT '存储文件名',
    stored_path VARCHAR(1024) NOT NULL COMMENT '存储相对路径',
    content_type VARCHAR(255) COMMENT '文件类型',
    size_bytes BIGINT NOT NULL COMMENT '文件大小(字节)',
    sha256 VARCHAR(64) COMMENT '文件SHA256摘要',
    status VARCHAR(32) NOT NULL COMMENT '处理状态(PROCESSING/READY/FAILED)',
    chunk_count INT DEFAULT 0 COMMENT '分片数量',
    error_message VARCHAR(1024) COMMENT '失败原因',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    deleted TINYINT(1) DEFAULT 0 COMMENT '是否删除(软删除标记)',
    deleted_time DATETIME COMMENT '删除时间',
    KEY idx_kb_id (kb_id),
    KEY idx_kb_deleted (kb_id, deleted)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识库文档';
