SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- P2: FAQ知识库增强（新增类型与FAQ条目表）

-- 1) document_collection 增加知识库类型
ALTER TABLE tb_document_collection
    ADD COLUMN collection_type varchar(16) NOT NULL DEFAULT 'DOCUMENT' COMMENT '知识库类型: DOCUMENT/FAQ' AFTER id;

-- 2) 回填历史数据
UPDATE tb_document_collection
SET collection_type = 'DOCUMENT'
WHERE collection_type IS NULL OR collection_type = '';

-- 3) 增加类型索引
ALTER TABLE tb_document_collection
    ADD INDEX idx_collection_type (collection_type);

-- 4) FAQ条目表
CREATE TABLE IF NOT EXISTS tb_faq_item
(
    id            bigint UNSIGNED NOT NULL COMMENT '主键',
    collection_id bigint UNSIGNED NOT NULL COMMENT '知识库ID',
    question      varchar(1024) NOT NULL COMMENT '问题',
    answer_html   longtext NULL COMMENT '答案HTML',
    answer_text   longtext NULL COMMENT '答案纯文本',
    order_no      int NULL DEFAULT 0 COMMENT '排序',
    options       text NULL COMMENT '扩展项',
    created       datetime NULL COMMENT '创建时间',
    created_by    bigint UNSIGNED NULL COMMENT '创建人',
    modified      datetime NULL COMMENT '更新时间',
    modified_by   bigint UNSIGNED NULL COMMENT '更新人',
    PRIMARY KEY (id),
    INDEX idx_faq_collection_id (collection_id),
    INDEX idx_faq_collection_order (collection_id, order_no)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT ='FAQ条目';

SET FOREIGN_KEY_CHECKS = 1;
