-- 模型平台与RAG模型绑定核心表（首期手工维护，后续可迁移到 Flyway/Liquibase）

CREATE TABLE IF NOT EXISTS model_provider (
    id BIGSERIAL PRIMARY KEY,
    provider_code VARCHAR(64) NOT NULL,
    provider_name VARCHAR(100) NOT NULL,
    provider_type VARCHAR(50) NOT NULL,
    protocol_type VARCHAR(50) NOT NULL DEFAULT 'OPENAI_COMPATIBLE',
    base_url VARCHAR(500) NOT NULL,
    auth_type VARCHAR(50) NOT NULL DEFAULT 'BEARER_TOKEN',
    secret_ref VARCHAR(200),
    api_key_cipher TEXT,
    timeout_ms INTEGER NOT NULL DEFAULT 60000,
    priority INTEGER NOT NULL DEFAULT 100,
    enabled BOOLEAN NOT NULL DEFAULT TRUE,
    health_status VARCHAR(50) NOT NULL DEFAULT 'UNKNOWN',
    last_health_check_time TIMESTAMP,
    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_model_provider_code UNIQUE (provider_code)
);

CREATE TABLE IF NOT EXISTS model_config (
    id BIGSERIAL PRIMARY KEY,
    provider_id BIGINT NOT NULL,
    model_code VARCHAR(100) NOT NULL,
    model_name VARCHAR(200) NOT NULL,
    upstream_model VARCHAR(200) NOT NULL,
    model_type VARCHAR(50) NOT NULL,
    context_window INTEGER,
    max_output_tokens INTEGER,
    embedding_dimension INTEGER,
    input_price_per_1k NUMERIC(12, 8),
    output_price_per_1k NUMERIC(12, 8),
    local_model BOOLEAN NOT NULL DEFAULT FALSE,
    default_model BOOLEAN NOT NULL DEFAULT FALSE,
    capabilities_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    options_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    enabled BOOLEAN NOT NULL DEFAULT TRUE,
    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_model_config_provider_code UNIQUE (provider_id, model_code),
    CONSTRAINT fk_model_config_provider_id FOREIGN KEY (provider_id) REFERENCES model_provider (id)
);

CREATE TABLE IF NOT EXISTS model_route_rule (
    id BIGSERIAL PRIMARY KEY,
    route_code VARCHAR(100) NOT NULL,
    route_name VARCHAR(100) NOT NULL,
    task_type VARCHAR(50) NOT NULL,
    match_scope VARCHAR(50) NOT NULL DEFAULT 'GLOBAL',
    scope_id BIGINT,
    primary_model_id BIGINT NOT NULL,
    fallback_model_ids_json JSONB NOT NULL DEFAULT '[]'::jsonb,
    route_strategy VARCHAR(50) NOT NULL DEFAULT 'MANUAL',
    max_latency_ms INTEGER,
    enabled BOOLEAN NOT NULL DEFAULT TRUE,
    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_model_route_rule_code UNIQUE (route_code),
    CONSTRAINT fk_model_route_primary_model_id FOREIGN KEY (primary_model_id) REFERENCES model_config (id)
);

CREATE TABLE IF NOT EXISTS rag_store_model_config (
    id BIGSERIAL PRIMARY KEY,
    store_id BIGINT NOT NULL,
    embedding_model_id BIGINT NOT NULL,
    embedding_dimension INTEGER NOT NULL DEFAULT 1024,
    chunk_strategy INTEGER,
    chunk_size INTEGER,
    chunk_overlap INTEGER,
    delimiter VARCHAR(50),
    active BOOLEAN NOT NULL DEFAULT TRUE,
    index_version INTEGER NOT NULL DEFAULT 1,
    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_store_model_config_store_active UNIQUE (store_id, active),
    CONSTRAINT fk_rag_store_model_config_store_id FOREIGN KEY (store_id) REFERENCES rag_store (id),
    CONSTRAINT fk_rag_store_model_config_embedding_model_id FOREIGN KEY (embedding_model_id) REFERENCES model_config (id)
);

CREATE TABLE IF NOT EXISTS agent_definition (
    id BIGSERIAL PRIMARY KEY,
    agent_code VARCHAR(100) NOT NULL,
    agent_name VARCHAR(200) NOT NULL,
    system_prompt TEXT,
    store_id BIGINT NOT NULL,
    status VARCHAR(50) NOT NULL DEFAULT 'ENABLED',
    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_agent_definition_code UNIQUE (agent_code),
    CONSTRAINT fk_agent_definition_store_id FOREIGN KEY (store_id) REFERENCES rag_store (id)
);

CREATE TABLE IF NOT EXISTS model_call_log (
    id BIGSERIAL PRIMARY KEY,
    request_id VARCHAR(64) NOT NULL,
    provider_id BIGINT,
    model_id BIGINT,
    task_type VARCHAR(50) NOT NULL,
    biz_type VARCHAR(50),
    biz_id VARCHAR(100),
    call_type VARCHAR(50) NOT NULL,
    status VARCHAR(50) NOT NULL,
    prompt_tokens INTEGER,
    completion_tokens INTEGER,
    total_tokens INTEGER,
    estimated_cost NUMERIC(14, 8),
    duration_ms INTEGER,
    request_hash VARCHAR(64),
    error_code VARCHAR(100),
    error_message VARCHAR(1000),
    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_model_call_log_request_id UNIQUE (request_id)
);

COMMENT ON TABLE model_provider IS '模型服务商配置表';
COMMENT ON COLUMN model_provider.id IS 'ID';
COMMENT ON COLUMN model_provider.provider_code IS '服务商编码';
COMMENT ON COLUMN model_provider.provider_name IS '服务商名称';
COMMENT ON COLUMN model_provider.provider_type IS '服务商类型';
COMMENT ON COLUMN model_provider.protocol_type IS '协议类型';
COMMENT ON COLUMN model_provider.base_url IS '服务基础地址';
COMMENT ON COLUMN model_provider.auth_type IS '鉴权类型';
COMMENT ON COLUMN model_provider.secret_ref IS '密钥环境变量引用';
COMMENT ON COLUMN model_provider.api_key_cipher IS '密钥密文';
COMMENT ON COLUMN model_provider.timeout_ms IS '超时时间(毫秒)';
COMMENT ON COLUMN model_provider.priority IS '优先级';
COMMENT ON COLUMN model_provider.enabled IS '是否启用';
COMMENT ON COLUMN model_provider.health_status IS '健康状态';
COMMENT ON COLUMN model_provider.last_health_check_time IS '最近健康检查时间';
COMMENT ON COLUMN model_provider.version IS '版本';
COMMENT ON COLUMN model_provider.create_time IS '创建时间';
COMMENT ON COLUMN model_provider.update_time IS '更新时间';
COMMENT ON COLUMN model_provider.remark IS '备注';
COMMENT ON COLUMN model_provider.create_by IS '创建者';
COMMENT ON COLUMN model_provider.update_by IS '更新者';

COMMENT ON TABLE model_config IS '模型配置表';
COMMENT ON COLUMN model_config.id IS 'ID';
COMMENT ON COLUMN model_config.provider_id IS '服务商ID';
COMMENT ON COLUMN model_config.model_code IS '模型编码';
COMMENT ON COLUMN model_config.model_name IS '模型名称';
COMMENT ON COLUMN model_config.upstream_model IS '上游模型名称';
COMMENT ON COLUMN model_config.model_type IS '模型类型';
COMMENT ON COLUMN model_config.context_window IS '上下文窗口大小';
COMMENT ON COLUMN model_config.max_output_tokens IS '最大输出Token数';
COMMENT ON COLUMN model_config.embedding_dimension IS '向量维度';
COMMENT ON COLUMN model_config.input_price_per_1k IS '输入千Token单价';
COMMENT ON COLUMN model_config.output_price_per_1k IS '输出千Token单价';
COMMENT ON COLUMN model_config.local_model IS '是否本地模型';
COMMENT ON COLUMN model_config.default_model IS '是否默认模型';
COMMENT ON COLUMN model_config.capabilities_json IS '能力配置JSON';
COMMENT ON COLUMN model_config.options_json IS '扩展选项JSON';
COMMENT ON COLUMN model_config.enabled IS '是否启用';
COMMENT ON COLUMN model_config.version IS '版本';
COMMENT ON COLUMN model_config.create_time IS '创建时间';
COMMENT ON COLUMN model_config.update_time IS '更新时间';
COMMENT ON COLUMN model_config.remark IS '备注';
COMMENT ON COLUMN model_config.create_by IS '创建者';
COMMENT ON COLUMN model_config.update_by IS '更新者';

COMMENT ON TABLE model_route_rule IS '模型路由规则表';
COMMENT ON COLUMN model_route_rule.id IS 'ID';
COMMENT ON COLUMN model_route_rule.route_code IS '路由规则编码';
COMMENT ON COLUMN model_route_rule.route_name IS '路由规则名称';
COMMENT ON COLUMN model_route_rule.task_type IS '任务类型';
COMMENT ON COLUMN model_route_rule.match_scope IS '匹配范围';
COMMENT ON COLUMN model_route_rule.scope_id IS '匹配范围业务ID';
COMMENT ON COLUMN model_route_rule.primary_model_id IS '主模型ID';
COMMENT ON COLUMN model_route_rule.fallback_model_ids_json IS '降级模型ID列表JSON';
COMMENT ON COLUMN model_route_rule.route_strategy IS '路由策略';
COMMENT ON COLUMN model_route_rule.max_latency_ms IS '最大延迟限制(毫秒)';
COMMENT ON COLUMN model_route_rule.enabled IS '是否启用';
COMMENT ON COLUMN model_route_rule.version IS '版本';
COMMENT ON COLUMN model_route_rule.create_time IS '创建时间';
COMMENT ON COLUMN model_route_rule.update_time IS '更新时间';
COMMENT ON COLUMN model_route_rule.remark IS '备注';
COMMENT ON COLUMN model_route_rule.create_by IS '创建者';
COMMENT ON COLUMN model_route_rule.update_by IS '更新者';

COMMENT ON TABLE rag_store_model_config IS '知识库模型配置表';
COMMENT ON COLUMN rag_store_model_config.id IS 'ID';
COMMENT ON COLUMN rag_store_model_config.store_id IS '知识库ID';
COMMENT ON COLUMN rag_store_model_config.embedding_model_id IS 'Embedding模型ID';
COMMENT ON COLUMN rag_store_model_config.embedding_dimension IS '向量维度';
COMMENT ON COLUMN rag_store_model_config.chunk_strategy IS '切片策略';
COMMENT ON COLUMN rag_store_model_config.chunk_size IS '切片大小';
COMMENT ON COLUMN rag_store_model_config.chunk_overlap IS '切片重叠大小';
COMMENT ON COLUMN rag_store_model_config.delimiter IS '切片分隔符';
COMMENT ON COLUMN rag_store_model_config.active IS '是否生效';
COMMENT ON COLUMN rag_store_model_config.index_version IS '索引版本号';
COMMENT ON COLUMN rag_store_model_config.version IS '版本';
COMMENT ON COLUMN rag_store_model_config.create_time IS '创建时间';
COMMENT ON COLUMN rag_store_model_config.update_time IS '更新时间';
COMMENT ON COLUMN rag_store_model_config.remark IS '备注';
COMMENT ON COLUMN rag_store_model_config.create_by IS '创建者';
COMMENT ON COLUMN rag_store_model_config.update_by IS '更新者';

COMMENT ON TABLE agent_definition IS 'Agent定义表';
COMMENT ON COLUMN agent_definition.id IS 'ID';
COMMENT ON COLUMN agent_definition.agent_code IS 'Agent编码';
COMMENT ON COLUMN agent_definition.agent_name IS 'Agent名称';
COMMENT ON COLUMN agent_definition.system_prompt IS '系统提示词';
COMMENT ON COLUMN agent_definition.store_id IS '绑定知识库ID';
COMMENT ON COLUMN agent_definition.status IS '状态';
COMMENT ON COLUMN agent_definition.version IS '版本';
COMMENT ON COLUMN agent_definition.create_time IS '创建时间';
COMMENT ON COLUMN agent_definition.update_time IS '更新时间';
COMMENT ON COLUMN agent_definition.remark IS '备注';
COMMENT ON COLUMN agent_definition.create_by IS '创建者';
COMMENT ON COLUMN agent_definition.update_by IS '更新者';

COMMENT ON TABLE model_call_log IS '模型调用日志表';
COMMENT ON COLUMN model_call_log.id IS 'ID';
COMMENT ON COLUMN model_call_log.request_id IS '请求唯一ID';
COMMENT ON COLUMN model_call_log.provider_id IS '服务商ID';
COMMENT ON COLUMN model_call_log.model_id IS '模型ID';
COMMENT ON COLUMN model_call_log.task_type IS '任务类型';
COMMENT ON COLUMN model_call_log.biz_type IS '业务类型';
COMMENT ON COLUMN model_call_log.biz_id IS '业务ID';
COMMENT ON COLUMN model_call_log.call_type IS '调用类型';
COMMENT ON COLUMN model_call_log.status IS '调用状态';
COMMENT ON COLUMN model_call_log.prompt_tokens IS '输入Token数';
COMMENT ON COLUMN model_call_log.completion_tokens IS '输出Token数';
COMMENT ON COLUMN model_call_log.total_tokens IS '总Token数';
COMMENT ON COLUMN model_call_log.estimated_cost IS '预估成本';
COMMENT ON COLUMN model_call_log.duration_ms IS '耗时(毫秒)';
COMMENT ON COLUMN model_call_log.request_hash IS '请求哈希';
COMMENT ON COLUMN model_call_log.error_code IS '错误码';
COMMENT ON COLUMN model_call_log.error_message IS '错误信息摘要';
COMMENT ON COLUMN model_call_log.version IS '版本';
COMMENT ON COLUMN model_call_log.create_time IS '创建时间';
COMMENT ON COLUMN model_call_log.update_time IS '更新时间';
COMMENT ON COLUMN model_call_log.remark IS '备注';
COMMENT ON COLUMN model_call_log.create_by IS '创建者';
COMMENT ON COLUMN model_call_log.update_by IS '更新者';
