-- prompts-core 核心表（pgsql）
-- 说明：字段风格尽量与参考项目一致（tenant/creator/updater/created_at/updated_at/deleted_at）

-- prompts_model_prompt 模型提示词配置表
CREATE TABLE IF NOT EXISTS prompts_model_prompt (
    -- 基础字段（与 common/db/gfdb 的 Hook 约定保持一致）
    id BIGINT PRIMARY KEY,                 -- 主键ID（非自增）
    tenant_id BIGINT NOT NULL DEFAULT 0,   -- 租户ID
    creator VARCHAR(64) NOT NULL,          -- 创建人
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间
    updater VARCHAR(64) NOT NULL,          -- 更新人
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 更新时间
    deleted_at TIMESTAMP(6),               -- 删除时间（软删）

    -- 业务字段（按你当前的最小字段集）
    model_type_id INT NOT NULL DEFAULT 0,  -- 模型分类ID
    model_type VARCHAR(64) NOT NULL,       -- 模型类别
    prompt_info JSONB NOT NULL DEFAULT '{}'::jsonb, -- 提示词信息（JSON）
    response_json_schema JSONB NOT NULL DEFAULT '{}'::jsonb, -- 模型返回表单 JSON 格式约束
    enabled SMALLINT NOT NULL DEFAULT 1,   -- 是否启用：1启用/0禁用
    version VARCHAR(64) NOT NULL DEFAULT '' -- 版本号（预留）
);

CREATE INDEX IF NOT EXISTS idx_prompts_model_prompt_tenant_id ON prompts_model_prompt(tenant_id);
CREATE INDEX IF NOT EXISTS idx_prompts_model_prompt_model_type_id ON prompts_model_prompt(model_type_id);
CREATE INDEX IF NOT EXISTS idx_prompts_model_prompt_model_type ON prompts_model_prompt(model_type);
CREATE INDEX IF NOT EXISTS idx_prompts_model_prompt_enabled ON prompts_model_prompt(enabled);
CREATE INDEX IF NOT EXISTS idx_prompts_model_prompt_deleted_at ON prompts_model_prompt(deleted_at);

COMMENT ON TABLE prompts_model_prompt IS '模型提示词配置表';
COMMENT ON COLUMN prompts_model_prompt.id IS '主键ID（非自增）';
COMMENT ON COLUMN prompts_model_prompt.tenant_id IS '租户ID';
COMMENT ON COLUMN prompts_model_prompt.creator IS '创建人';
COMMENT ON COLUMN prompts_model_prompt.created_at IS '创建时间';
COMMENT ON COLUMN prompts_model_prompt.updater IS '更新人';
COMMENT ON COLUMN prompts_model_prompt.updated_at IS '更新时间';
COMMENT ON COLUMN prompts_model_prompt.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN prompts_model_prompt.model_type_id IS '模型分类ID';
COMMENT ON COLUMN prompts_model_prompt.model_type IS '模型类别';
COMMENT ON COLUMN prompts_model_prompt.prompt_info IS '提示词信息（JSON）';
COMMENT ON COLUMN prompts_model_prompt.response_json_schema IS '模型返回表单 JSON 格式约束';
COMMENT ON COLUMN prompts_model_prompt.enabled IS '是否启用：1启用/0禁用';
COMMENT ON COLUMN prompts_model_prompt.version IS '版本号（预留）';

-- prompts_compose_task 拼接提示词任务记录表
CREATE TABLE IF NOT EXISTS prompts_compose_task (
    id BIGINT PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(64) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP(6),

    task_id VARCHAR(64) NOT NULL,
    model_name VARCHAR(128) NOT NULL DEFAULT '',
    skill_name VARCHAR(128) NOT NULL DEFAULT '',
    gateway_state INT NOT NULL DEFAULT 0,
    limit_words INT NOT NULL DEFAULT 0,
    request_payload JSONB NOT NULL DEFAULT '{}'::jsonb,
    result_text TEXT NOT NULL DEFAULT '',
    messages JSONB NOT NULL DEFAULT '[]'::jsonb,
    status VARCHAR(32) NOT NULL DEFAULT 'pending',
    error_message TEXT NOT NULL DEFAULT '',
    oss_file VARCHAR(1024) NOT NULL DEFAULT '',
    file_type VARCHAR(64) NOT NULL DEFAULT ''
);

CREATE UNIQUE INDEX IF NOT EXISTS uk_prompts_compose_task_task_id ON prompts_compose_task(task_id);
CREATE INDEX IF NOT EXISTS idx_prompts_compose_task_status ON prompts_compose_task(status);
CREATE INDEX IF NOT EXISTS idx_prompts_compose_task_deleted_at ON prompts_compose_task(deleted_at);

COMMENT ON TABLE prompts_compose_task IS '拼接提示词任务记录表';
COMMENT ON COLUMN prompts_compose_task.task_id IS 'model-gateway 任务ID';
COMMENT ON COLUMN prompts_compose_task.model_name IS '业务模型名称';
COMMENT ON COLUMN prompts_compose_task.skill_name IS '技能名称';
COMMENT ON COLUMN prompts_compose_task.gateway_state IS 'model-gateway 状态：0排队/1执行/2成功/3失败/4已下载';
COMMENT ON COLUMN prompts_compose_task.limit_words IS '提示词限制字数';
COMMENT ON COLUMN prompts_compose_task.request_payload IS '发给 model-gateway 的请求内容';
COMMENT ON COLUMN prompts_compose_task.result_text IS '回调返回的文本结果';
COMMENT ON COLUMN prompts_compose_task.messages IS '最终解析后的 messages';
COMMENT ON COLUMN prompts_compose_task.status IS '业务状态：pending/success/failed';
COMMENT ON COLUMN prompts_compose_task.error_message IS '业务错误信息';
COMMENT ON COLUMN prompts_compose_task.oss_file IS '网关返回的结果文件地址';
COMMENT ON COLUMN prompts_compose_task.file_type IS '结果文件类型';

-- prompts_compose_session 提示词历史会话表
CREATE TABLE IF NOT EXISTS prompts_compose_session (
    id BIGINT PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(64) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP(6),

    session_id VARCHAR(64) NOT NULL,
    request_content JSONB NOT NULL DEFAULT '[]'::jsonb,
    response_content JSONB NOT NULL DEFAULT '[]'::jsonb,
    remark VARCHAR(500) NOT NULL DEFAULT ''
);

CREATE INDEX IF NOT EXISTS idx_prompts_compose_session_session_id ON prompts_compose_session(session_id);
CREATE INDEX IF NOT EXISTS idx_prompts_compose_session_deleted_at ON prompts_compose_session(deleted_at);

COMMENT ON TABLE prompts_compose_session IS '提示词历史会话表';
COMMENT ON COLUMN prompts_compose_session.id IS '主键ID（非自增）';
COMMENT ON COLUMN prompts_compose_session.tenant_id IS '租户ID';
COMMENT ON COLUMN prompts_compose_session.creator IS '创建人';
COMMENT ON COLUMN prompts_compose_session.created_at IS '创建时间';
COMMENT ON COLUMN prompts_compose_session.updater IS '更新人';
COMMENT ON COLUMN prompts_compose_session.updated_at IS '更新时间';
COMMENT ON COLUMN prompts_compose_session.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN prompts_compose_session.session_id IS '会话ID';
COMMENT ON COLUMN prompts_compose_session.request_content IS '请求内容（JSON格式）';
COMMENT ON COLUMN prompts_compose_session.response_content IS '返回内容（JSON格式）';
COMMENT ON COLUMN prompts_compose_session.remark IS '备注';