-- 合规审核页面配置（供 biz 页面与 open-api 运行时读取）

-- PostgreSQL
CREATE TABLE IF NOT EXISTS d_content_audit_setting (
    id                  VARCHAR(64)   NOT NULL,
    scope_code          VARCHAR(64)   NOT NULL,
    engine_running      SMALLINT      NOT NULL DEFAULT 1,
    prompt_enabled      SMALLINT      NOT NULL DEFAULT 1,
    answer_enabled      SMALLINT      NOT NULL DEFAULT 1,
    reasoning_enabled   SMALLINT      NOT NULL DEFAULT 0,
    recall_enabled      SMALLINT      NOT NULL DEFAULT 0,
    reject_code         INT           NOT NULL DEFAULT 403,
    reject_msg          VARCHAR(255)  NOT NULL DEFAULT '内容不合规',
    reject_action       VARCHAR(32)   NOT NULL DEFAULT 'blocked',
    create_by           VARCHAR(64)   DEFAULT '',
    create_time         TIMESTAMP,
    update_by           VARCHAR(64)   DEFAULT '',
    update_time         TIMESTAMP,
    is_deleted          INT           NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    CONSTRAINT uk_content_audit_setting_scope UNIQUE (scope_code)
);

CREATE INDEX IF NOT EXISTS idx_content_audit_setting_scope_del
    ON d_content_audit_setting(scope_code, is_deleted);

CREATE TABLE IF NOT EXISTS d_content_corpus (
    id                  VARCHAR(64)   NOT NULL,
    scope_code          VARCHAR(64)   NOT NULL,
    corpus_text         VARCHAR(500)  NOT NULL,
    tag                 VARCHAR(64)   DEFAULT '',
    expire_date         DATE          NULL,
    status              VARCHAR(20)   NOT NULL DEFAULT 'ENABLED',
    create_by           VARCHAR(64)   DEFAULT '',
    create_time         TIMESTAMP,
    update_by           VARCHAR(64)   DEFAULT '',
    update_time         TIMESTAMP,
    is_deleted          INT           NOT NULL DEFAULT 0,
    PRIMARY KEY (id)
);

CREATE INDEX IF NOT EXISTS idx_content_corpus_scope_status
    ON d_content_corpus(scope_code, status, is_deleted);

-- 默认初始化 GLOBAL 配置
INSERT INTO d_content_audit_setting(id, scope_code, engine_running, prompt_enabled, answer_enabled, reasoning_enabled, recall_enabled, reject_code, reject_msg, reject_action, create_by, create_time, update_by, update_time, is_deleted)
SELECT 'content_audit_global_init', 'GLOBAL', 1, 1, 1, 0, 0, 403, '内容不合规', 'blocked', 'admin', CURRENT_TIMESTAMP, 'admin', CURRENT_TIMESTAMP, 0
WHERE NOT EXISTS (SELECT 1 FROM d_content_audit_setting WHERE scope_code = 'GLOBAL' AND is_deleted = 0);

-- MySQL 参考（按需执行）
-- CREATE TABLE IF NOT EXISTS d_content_audit_setting (
--   id                VARCHAR(64)   NOT NULL,
--   scope_code        VARCHAR(64)   NOT NULL,
--   engine_running    TINYINT(1)    NOT NULL DEFAULT 1,
--   prompt_enabled    TINYINT(1)    NOT NULL DEFAULT 1,
--   answer_enabled    TINYINT(1)    NOT NULL DEFAULT 1,
--   reasoning_enabled TINYINT(1)    NOT NULL DEFAULT 0,
--   recall_enabled    TINYINT(1)    NOT NULL DEFAULT 0,
--   reject_code       INT           NOT NULL DEFAULT 403,
--   reject_msg        VARCHAR(255)  NOT NULL DEFAULT '内容不合规',
--   reject_action     VARCHAR(32)   NOT NULL DEFAULT 'blocked',
--   create_by         VARCHAR(64)   DEFAULT '',
--   create_time       DATETIME,
--   update_by         VARCHAR(64)   DEFAULT '',
--   update_time       DATETIME,
--   is_deleted        INT           NOT NULL DEFAULT 0,
--   PRIMARY KEY (id),
--   UNIQUE KEY uk_content_audit_setting_scope (scope_code),
--   KEY idx_content_audit_setting_scope_del (scope_code, is_deleted)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- CREATE TABLE IF NOT EXISTS d_content_corpus (
--   id                VARCHAR(64)   NOT NULL,
--   scope_code        VARCHAR(64)   NOT NULL,
--   corpus_text       VARCHAR(500)  NOT NULL,
--   tag               VARCHAR(64)   DEFAULT '',
--   expire_date       DATE          NULL,
--   status            VARCHAR(20)   NOT NULL DEFAULT 'ENABLED',
--   create_by         VARCHAR(64)   DEFAULT '',
--   create_time       DATETIME,
--   update_by         VARCHAR(64)   DEFAULT '',
--   update_time       DATETIME,
--   is_deleted        INT           NOT NULL DEFAULT 0,
--   PRIMARY KEY (id),
--   KEY idx_content_corpus_scope_status (scope_code, status, is_deleted)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
