DROP TABLE IF EXISTS rag_document;

CREATE TABLE rag_document (
    id BIGSERIAL PRIMARY KEY,
    store_id BIGINT NOT NULL,
    attachment_id BIGINT NOT NULL,
    document_title VARCHAR(255) NOT NULL,
    document_summary VARCHAR(1000) DEFAULT '',
    parse_status VARCHAR(50) NOT NULL DEFAULT 'UPLOADED',
    index_status VARCHAR(50) NOT NULL DEFAULT 'PENDING',
    enabled BOOLEAN NOT NULL DEFAULT TRUE,
    error_message VARCHAR(1000) DEFAULT '',
    version INTEGER NOT NULL DEFAULT 1,
    create_time TIMESTAMP,
    update_time TIMESTAMP,
    remark VARCHAR(500) DEFAULT '',
    create_by VARCHAR(64),
    update_by VARCHAR(64),
    CONSTRAINT uk_rag_document_attachment UNIQUE (attachment_id),
    CONSTRAINT fk_rag_document_store_id FOREIGN KEY (store_id) REFERENCES rag_store (id),
    CONSTRAINT fk_rag_document_attachment_id FOREIGN KEY (attachment_id) REFERENCES sys_attachment (id)
);

CREATE INDEX idx_rag_document_store_id ON rag_document (store_id);
CREATE INDEX idx_rag_document_parse_status ON rag_document (parse_status);
CREATE INDEX idx_rag_document_index_status ON rag_document (index_status);
CREATE INDEX idx_rag_document_enabled ON rag_document (enabled);

COMMENT ON TABLE rag_document IS 'RAG知识库文档表';
COMMENT ON COLUMN rag_document.id IS 'ID';
COMMENT ON COLUMN rag_document.store_id IS '知识库ID';
COMMENT ON COLUMN rag_document.attachment_id IS '附件ID';
COMMENT ON COLUMN rag_document.document_title IS '文档标题';
COMMENT ON COLUMN rag_document.document_summary IS '文档摘要';
COMMENT ON COLUMN rag_document.parse_status IS '解析状态';
COMMENT ON COLUMN rag_document.index_status IS '索引状态';
COMMENT ON COLUMN rag_document.enabled IS '是否启用';
COMMENT ON COLUMN rag_document.error_message IS '失败原因';
COMMENT ON COLUMN rag_document.version IS '版本';
COMMENT ON COLUMN rag_document.create_time IS '创建时间';
COMMENT ON COLUMN rag_document.update_time IS '更新时间';
COMMENT ON COLUMN rag_document.remark IS '备注';
COMMENT ON COLUMN rag_document.create_by IS '创建者';
COMMENT ON COLUMN rag_document.update_by IS '更新者';
