-- =========================
-- model_gateway_models
-- =========================
CREATE TABLE IF NOT EXISTS model_gateway_models (
                                                    id              int8 PRIMARY KEY,
                                                    tenant_id       int8 NOT NULL DEFAULT 0,
                                                    creator         varchar(64) NOT NULL,
    created_at      timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater         varchar(64) NOT NULL,
    updated_at      timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at      timestamp(6),
    model_name      varchar(128) NOT NULL,
    model_type      int2 NOT NULL DEFAULT 0,
    operator_name   varchar(64) NOT NULL DEFAULT '',
    base_url        varchar(256) NOT NULL,
    http_method     varchar(8) NOT NULL DEFAULT 'POST',
    head_msg        jsonb NOT NULL DEFAULT '{}',
    api_key         varchar(256) NOT NULL DEFAULT '',
    is_private      int2 NOT NULL DEFAULT 0,
    enabled         int2 NOT NULL DEFAULT 1,
    is_chat_model   int2 NOT NULL DEFAULT 0,
    is_owner        int2 NOT NULL DEFAULT 99,
    form_json       jsonb NOT NULL DEFAULT '{}',
    request_mapping  jsonb NOT NULL DEFAULT '{}',
    response_mapping jsonb NOT NULL DEFAULT '{}',
    response_body   varchar(128) NOT NULL DEFAULT '',
    token_config    jsonb NOT NULL DEFAULT '{}',
    extend_mapping  jsonb NOT NULL DEFAULT '{}',
    query_config    jsonb NOT NULL DEFAULT '{}',
    stream_config   jsonb NOT NULL DEFAULT '{}',
    first_frame     varchar(128) NOT NULL DEFAULT '',
    last_frame      varchar(128) NOT NULL DEFAULT '',
    max_concurrency int4 NOT NULL DEFAULT 10,
    timeout_seconds int4 NOT NULL DEFAULT 600,
    retry_times     int2 NOT NULL DEFAULT 3,
    auto_clean_seconds int4 NOT NULL DEFAULT 86400,
    response_token_field varchar(128) NOT NULL DEFAULT '',
    call_mode       int2 NOT NULL DEFAULT 0,
    required_fields jsonb NOT NULL DEFAULT '[]',
    max_tokens      int4 DEFAULT 0
    );

CREATE UNIQUE INDEX IF NOT EXISTS uk_model_gateway_models_tenant_creator_model ON model_gateway_models (tenant_id, creator, model_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_model_name ON model_gateway_models (model_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_model_type ON model_gateway_models (model_type);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_tenant_id ON model_gateway_models (tenant_id);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_deleted_at ON model_gateway_models (deleted_at);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_enabled ON model_gateway_models (enabled);

COMMENT ON TABLE model_gateway_models IS '模型配置表';
COMMENT ON COLUMN model_gateway_models.id IS '主键ID(非自增)';
COMMENT ON COLUMN model_gateway_models.tenant_id IS '租户ID';
COMMENT ON COLUMN model_gateway_models.creator IS '创建人';
COMMENT ON COLUMN model_gateway_models.created_at IS '创建时间';
COMMENT ON COLUMN model_gateway_models.updater IS '更新人';
COMMENT ON COLUMN model_gateway_models.updated_at IS '更新时间';
COMMENT ON COLUMN model_gateway_models.deleted_at IS '删除时间(软删)';
COMMENT ON COLUMN model_gateway_models.model_name IS '模型名称';
COMMENT ON COLUMN model_gateway_models.model_type IS '模型类型';
COMMENT ON COLUMN model_gateway_models.operator_name IS '运营商名称';
COMMENT ON COLUMN model_gateway_models.base_url IS '模型地址';
COMMENT ON COLUMN model_gateway_models.http_method IS '请求方式 GET/POST';
COMMENT ON COLUMN model_gateway_models.head_msg IS '请求头信息';
COMMENT ON COLUMN model_gateway_models.api_key IS '调用凭证/密钥';

COMMENT ON COLUMN model_gateway_models.is_private IS '是否私有化：0-私有 1-公共';
COMMENT ON COLUMN model_gateway_models.enabled IS '是否启用：0-停用 1-启用';
COMMENT ON COLUMN model_gateway_models.is_chat_model IS '是否为对话模型：0-否 1-是';
COMMENT ON COLUMN model_gateway_models.is_owner IS '1=当前用户创建 0=超级管理员';

COMMENT ON COLUMN model_gateway_models.form_json IS '动态表单结构';
COMMENT ON COLUMN model_gateway_models.request_mapping IS '请求映射';
COMMENT ON COLUMN model_gateway_models.response_mapping IS '返回映射';
COMMENT ON COLUMN model_gateway_models.response_body IS '返回主体';
COMMENT ON COLUMN model_gateway_models.token_config IS 'Token计算配置';
COMMENT ON COLUMN model_gateway_models.extend_mapping IS '附加映射';
COMMENT ON COLUMN model_gateway_models.query_config IS '查询/回调配置';
COMMENT ON COLUMN model_gateway_models.stream_config IS '流式输出配置';
COMMENT ON COLUMN model_gateway_models.first_frame IS '首帧图片参数';
COMMENT ON COLUMN model_gateway_models.last_frame IS '尾帧图片参数';
COMMENT ON COLUMN model_gateway_models.max_concurrency IS '最大并发数';
COMMENT ON COLUMN model_gateway_models.timeout_seconds IS '调用模型超时(秒)';
COMMENT ON COLUMN model_gateway_models.retry_times IS '失败重试次数';
COMMENT ON COLUMN model_gateway_models.auto_clean_seconds IS '任务完成后自动清理时间(秒)';
COMMENT ON COLUMN model_gateway_models.response_token_field IS '响应中消耗token的字段映射';
COMMENT ON COLUMN model_gateway_models.call_mode IS '调用模式：0-同步 1-异步 2-流式';
COMMENT ON COLUMN model_gateway_models.required_fields IS '必选字段列表';
COMMENT ON COLUMN model_gateway_models.max_tokens IS '最大 token 数，0 表示不传';



-- =========================
-- model_gateway_task
-- =========================
CREATE TABLE IF NOT EXISTS model_gateway_task (
                                                  id              int8 PRIMARY KEY,
                                                  tenant_id       int8 NOT NULL DEFAULT 0,
                                                  creator         varchar(64) NOT NULL,
    created_at      timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater         varchar(64) NOT NULL,
    updated_at      timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at      timestamp(6),
    model_name      varchar(128) NOT NULL,
    task_id         varchar(64) NOT NULL,
    biz_name        varchar(128) NOT NULL DEFAULT '',
    callback_url    varchar(512) DEFAULT '',
    state           int2 NOT NULL DEFAULT 0,
    retry_count     int4 NOT NULL DEFAULT 0,
    phase           int2 NOT NULL DEFAULT 0,
    tmp_file        text DEFAULT '',
    error_msg       text DEFAULT '',
    result_file     jsonb NOT NULL DEFAULT '{}',
    request_payload jsonb NOT NULL DEFAULT '{}',
    text_result     jsonb NOT NULL DEFAULT '{}',
    expend_tokens   int8 NOT NULL DEFAULT 0,
    duration_seconds int8 NOT NULL DEFAULT 0,
    epicycle_id     varchar(64) NOT NULL DEFAULT ''
    );

CREATE UNIQUE INDEX IF NOT EXISTS uk_model_gateway_task_tenant_creator_task_id ON model_gateway_task (tenant_id, creator, task_id);
CREATE INDEX IF NOT EXISTS idx_model_gateway_task_task_id ON model_gateway_task (task_id);
CREATE INDEX IF NOT EXISTS idx_model_gateway_task_state ON model_gateway_task (state);
CREATE INDEX IF NOT EXISTS idx_model_gateway_task_deleted_at ON model_gateway_task (deleted_at);

COMMENT ON TABLE model_gateway_task IS '模型网关任务表';
COMMENT ON COLUMN model_gateway_task.id IS '主键ID';
COMMENT ON COLUMN model_gateway_task.tenant_id IS '租户ID';
COMMENT ON COLUMN model_gateway_task.creator IS '创建人';
COMMENT ON COLUMN model_gateway_task.created_at IS '创建时间';
COMMENT ON COLUMN model_gateway_task.updater IS '更新人';
COMMENT ON COLUMN model_gateway_task.updated_at IS '更新时间';
COMMENT ON COLUMN model_gateway_task.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN model_gateway_task.model_name IS '模型名称';
COMMENT ON COLUMN model_gateway_task.task_id IS '任务ID（对外返回）';
COMMENT ON COLUMN model_gateway_task.biz_name IS '业务名称（调用方模块/系统）';
COMMENT ON COLUMN model_gateway_task.callback_url IS '回调地址';
COMMENT ON COLUMN model_gateway_task.state IS '0排队中/1执行中/2成功/3失败/4已下载';
COMMENT ON COLUMN model_gateway_task.retry_count IS '已重试次数';
COMMENT ON COLUMN model_gateway_task.phase IS '执行阶段：0模型阶段/1OSS阶段';
COMMENT ON COLUMN model_gateway_task.tmp_file IS '临时结果文件路径';
COMMENT ON COLUMN model_gateway_task.error_msg IS '错误信息';
COMMENT ON COLUMN model_gateway_task.result_file IS '结果文件：{oss_file, file_type, file_size}';
COMMENT ON COLUMN model_gateway_task.request_payload IS '请求参数（JSON）';
COMMENT ON COLUMN model_gateway_task.text_result IS '文本类结果';
COMMENT ON COLUMN model_gateway_task.expend_tokens IS '消耗token数';
COMMENT ON COLUMN model_gateway_task.duration_seconds IS '耗时（秒）';
COMMENT ON COLUMN model_gateway_task.epicycle_id IS '轮次ID';



-- =========================
-- model_gateway_log_stat
-- =========================
CREATE TABLE IF NOT EXISTS model_gateway_log_stat (
                                                      day             date NOT NULL,
                                                      tenant_id       int8 NOT NULL DEFAULT 0,
                                                      creator         varchar(64) NOT NULL DEFAULT '',
    model_name      varchar(128) NOT NULL DEFAULT '',
    request_count   int8 NOT NULL DEFAULT 0,
    created_at      timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at      timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (day, tenant_id, creator, model_name)
    );

CREATE INDEX IF NOT EXISTS idx_model_gateway_log_stat_day ON model_gateway_log_stat (day);
CREATE INDEX IF NOT EXISTS idx_model_gateway_log_stat_creator ON model_gateway_log_stat (creator);
CREATE INDEX IF NOT EXISTS idx_model_gateway_log_stat_model_name ON model_gateway_log_stat (model_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_log_stat_tenant_day ON model_gateway_log_stat (tenant_id, day);

COMMENT ON TABLE model_gateway_log_stat IS '按天统计表';
COMMENT ON COLUMN model_gateway_log_stat.day IS '天（YYYY-MM-DD）';
COMMENT ON COLUMN model_gateway_log_stat.tenant_id IS '租户ID';
COMMENT ON COLUMN model_gateway_log_stat.creator IS '创建人';
COMMENT ON COLUMN model_gateway_log_stat.model_name IS '模型名称';
COMMENT ON COLUMN model_gateway_log_stat.request_count IS '请求次数';
COMMENT ON COLUMN model_gateway_log_stat.created_at IS '创建时间';
COMMENT ON COLUMN model_gateway_log_stat.updated_at IS '更新时间';


-- =========================
-- model_gateway_logs_op
-- =========================
CREATE TABLE IF NOT EXISTS model_gateway_logs_op (
                                                     id              int8 PRIMARY KEY,
                                                     tenant_id       int8 NOT NULL DEFAULT 0,
                                                     creator         varchar(64) NOT NULL,
    created_at      timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater         varchar(64) NOT NULL,
    updated_at      timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at      timestamp(6),
    ip              varchar(64) DEFAULT '',
    user_agent      varchar(256) DEFAULT '',
    api_path        varchar(256) DEFAULT '',
    http_method     varchar(16) DEFAULT '',
    biz_name        varchar(128) NOT NULL DEFAULT '',
    model_name      varchar(128) NOT NULL DEFAULT '',
    task_id         varchar(64) NOT NULL DEFAULT '',
    op_type         varchar(64) NOT NULL DEFAULT 'createTask',
    success         int2 NOT NULL DEFAULT 1,
    error_msg       text DEFAULT '',
    cost_ms         int8 NOT NULL DEFAULT 0,
    request_payload  jsonb,
    response_payload jsonb
    );

CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_task_id ON model_gateway_logs_op (task_id);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_biz_name ON model_gateway_logs_op (biz_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_model_name ON model_gateway_logs_op (model_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_op_type ON model_gateway_logs_op (op_type);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_deleted_at ON model_gateway_logs_op (deleted_at);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_tenant_time ON model_gateway_logs_op (tenant_id, created_at);

COMMENT ON TABLE model_gateway_logs_op IS '操作日志表';
COMMENT ON COLUMN model_gateway_logs_op.id IS '主键ID（非自增）';
COMMENT ON COLUMN model_gateway_logs_op.tenant_id IS '租户ID';
COMMENT ON COLUMN model_gateway_logs_op.creator IS '创建人';
COMMENT ON COLUMN model_gateway_logs_op.created_at IS '创建时间';
COMMENT ON COLUMN model_gateway_logs_op.updater IS '更新人';
COMMENT ON COLUMN model_gateway_logs_op.updated_at IS '更新时间';
COMMENT ON COLUMN model_gateway_logs_op.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN model_gateway_logs_op.ip IS '客户端IP';
COMMENT ON COLUMN model_gateway_logs_op.user_agent IS 'User-Agent';
COMMENT ON COLUMN model_gateway_logs_op.api_path IS '接口路径';
COMMENT ON COLUMN model_gateway_logs_op.http_method IS 'HTTP方法';
COMMENT ON COLUMN model_gateway_logs_op.biz_name IS '业务名称（调用方模块/系统）';
COMMENT ON COLUMN model_gateway_logs_op.model_name IS '模型名称';
COMMENT ON COLUMN model_gateway_logs_op.task_id IS '任务ID';
COMMENT ON COLUMN model_gateway_logs_op.op_type IS '操作类型';
COMMENT ON COLUMN model_gateway_logs_op.success IS '是否成功：1成功/0失败';
COMMENT ON COLUMN model_gateway_logs_op.error_msg IS '错误信息（失败时）';
COMMENT ON COLUMN model_gateway_logs_op.cost_ms IS '耗时（毫秒）';
COMMENT ON COLUMN model_gateway_logs_op.request_payload IS '请求 JSON';
COMMENT ON COLUMN model_gateway_logs_op.response_payload IS '响应 JSON';