-- 模型平台核心表。
-- 与 docs/MODEL_PROVIDER_SCHEMA.sql 保持一致，作为后续落库执行入口。

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 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)
);

CREATE INDEX IF NOT EXISTS idx_model_config_provider_id ON model_config (provider_id);
CREATE INDEX IF NOT EXISTS idx_model_config_model_type ON model_config (model_type);
CREATE INDEX IF NOT EXISTS idx_model_route_task_scope ON model_route_rule (task_type, match_scope, scope_id);
CREATE INDEX IF NOT EXISTS idx_rag_store_model_config_store_id ON rag_store_model_config (store_id);
CREATE INDEX IF NOT EXISTS idx_model_call_log_request_id ON model_call_log (request_id);
CREATE INDEX IF NOT EXISTS idx_model_call_log_task_type ON model_call_log (task_type);
