#!/bin/bash

set -e

# 初始化常见模型供应商和主流模型
# TODO: 将 API Key 移到环境变量或配置文件中，不要硬编码

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BASE_DIR="$(cd "$SCRIPT_DIR/../../.." && pwd)/base_platform"

cd "$BASE_DIR"

echo "正在初始化模型供应商和模型..."

# 初始化/修正供应商元数据
docker exec -i base_platform_postgres psql -U user -d base_platform <<'SQL' > /dev/null
UPDATE model_providers AS p
SET description = v.description,
    icon = v.icon,
    color = v.color,
    default_base_url = v.default_base_url,
    base_url = v.base_url,
    api_key = COALESCE(v.api_key, p.api_key),
    enabled = v.enabled,
    configured = v.configured,
    priority = v.priority,
    provider_kind = v.provider_kind,
    protocol = v.protocol,
    auth_type = v.auth_type,
    capabilities = v.capabilities,
    is_local = v.is_local,
    healthcheck_path = v.healthcheck_path,
    updated_at = NOW()
FROM (VALUES
  ('阿里云百炼', '通义千问系列模型', 'qwen', '#FF6A00', 'https://dashscope.aliyuncs.com/compatible-mode/v1', 'https://dashscope.aliyuncs.com/compatible-mode/v1', 'sk-5ae737dd5e3e40fea3c57619a5efe344', true, true, 4, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL),
  ('OpenAI', 'OpenAI GPT系列模型', 'openai', '#10A37F', 'https://api.openai.com/v1', 'https://api.openai.com/v1', NULL, true, false, 1, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL),
  ('Anthropic', 'Claude系列模型', 'anthropic', '#D97757', 'https://api.anthropic.com', 'https://api.anthropic.com', NULL, true, false, 2, 'public_api', 'anthropic', 'api_key', '["chat"]'::json, false, NULL),
  ('Google', 'Gemini系列模型', 'google', '#4285F4', 'https://generativelanguage.googleapis.com/v1beta', 'https://generativelanguage.googleapis.com/v1beta', NULL, true, false, 3, 'public_api', 'gemini', 'api_key', '["chat", "embedding"]'::json, false, NULL),
  ('智谱AI', 'GLM系列模型', 'zhipu', '#6366F1', 'https://open.bigmodel.cn/api/paas/v4', 'https://open.bigmodel.cn/api/paas/v4', NULL, true, false, 5, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL),
  ('DeepSeek', 'DeepSeek系列模型', 'deepseek', '#1E40AF', 'https://api.deepseek.com', 'https://api.deepseek.com', NULL, true, false, 6, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL),
  ('月之暗面', 'Moonshot系列模型', 'moonshot', '#8B5CF6', 'https://api.moonshot.cn/v1', 'https://api.moonshot.cn/v1', NULL, true, false, 7, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL)
) AS v(name, description, icon, color, default_base_url, base_url, api_key, enabled, configured, priority, provider_kind, protocol, auth_type, capabilities, is_local, healthcheck_path)
WHERE p.name = v.name;

INSERT INTO model_providers (
  name, description, icon, color, default_base_url, base_url, api_key,
  enabled, configured, priority, is_deleted, created_at, updated_at,
  provider_kind, protocol, auth_type, capabilities, is_local, healthcheck_path
)
SELECT
  v.name, v.description, v.icon, v.color, v.default_base_url, v.base_url, v.api_key,
  v.enabled, v.configured, v.priority, false, NOW(), NOW(),
  v.provider_kind, v.protocol, v.auth_type, v.capabilities, v.is_local, v.healthcheck_path
FROM (VALUES
  ('阿里云百炼', '通义千问系列模型', 'qwen', '#FF6A00', 'https://dashscope.aliyuncs.com/compatible-mode/v1', 'https://dashscope.aliyuncs.com/compatible-mode/v1', 'sk-5ae737dd5e3e40fea3c57619a5efe344', true, true, 4, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL),
  ('OpenAI', 'OpenAI GPT系列模型', 'openai', '#10A37F', 'https://api.openai.com/v1', 'https://api.openai.com/v1', NULL, true, false, 1, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL),
  ('Anthropic', 'Claude系列模型', 'anthropic', '#D97757', 'https://api.anthropic.com', 'https://api.anthropic.com', NULL, true, false, 2, 'public_api', 'anthropic', 'api_key', '["chat"]'::json, false, NULL),
  ('Google', 'Gemini系列模型', 'google', '#4285F4', 'https://generativelanguage.googleapis.com/v1beta', 'https://generativelanguage.googleapis.com/v1beta', NULL, true, false, 3, 'public_api', 'gemini', 'api_key', '["chat", "embedding"]'::json, false, NULL),
  ('智谱AI', 'GLM系列模型', 'zhipu', '#6366F1', 'https://open.bigmodel.cn/api/paas/v4', 'https://open.bigmodel.cn/api/paas/v4', NULL, true, false, 5, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL),
  ('DeepSeek', 'DeepSeek系列模型', 'deepseek', '#1E40AF', 'https://api.deepseek.com', 'https://api.deepseek.com', NULL, true, false, 6, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL),
  ('月之暗面', 'Moonshot系列模型', 'moonshot', '#8B5CF6', 'https://api.moonshot.cn/v1', 'https://api.moonshot.cn/v1', NULL, true, false, 7, 'public_api', 'openai_compatible', 'bearer', '["chat", "embedding"]'::json, false, NULL)
) AS v(name, description, icon, color, default_base_url, base_url, api_key, enabled, configured, priority, provider_kind, protocol, auth_type, capabilities, is_local, healthcheck_path)
WHERE NOT EXISTS (
  SELECT 1 FROM model_providers p WHERE p.name = v.name
);
SQL

# 插入模型
docker exec -i base_platform_postgres psql -U user -d base_platform <<'SQL' > /dev/null
INSERT INTO models (
  provider_id, code, name, type, enabled, is_active, is_deleted, created_at, updated_at,
  remote_model_id, supports_stream, supports_tools, priority
)
SELECT p.id, m.code, m.name, m.type, true, true, false, NOW(), NOW(), m.code, true, false, 100
FROM model_providers p
CROSS JOIN (VALUES
  ('阿里云百炼', 'qwen-max', 'Qwen Max', 'chat'),
  ('阿里云百炼', 'qwen-plus', 'Qwen Plus', 'chat'),
  ('阿里云百炼', 'qwen-turbo', 'Qwen Turbo', 'chat'),
  ('阿里云百炼', 'qwen3.5-122b-a10b', 'Qwen 3.5 122B', 'chat'),
  ('阿里云百炼', 'qwen3.5-plus', 'Qwen 3.5 Plus', 'chat'),
  ('阿里云百炼', 'qwen3.5-397b-a17b', 'Qwen 3.5 397B', 'chat'),
  ('阿里云百炼', 'text-embedding-v3', 'Text Embedding V3', 'embedding'),
  ('阿里云百炼', 'text-embedding-v4', 'Text Embedding V4', 'embedding'),
  ('阿里云百炼', 'qwen3-vl-embedding', 'Qwen3 VL Embedding', 'embedding')
) AS m(provider_name, code, name, type)
WHERE p.name = m.provider_name
  AND NOT EXISTS (SELECT 1 FROM models WHERE code = m.code);
SQL

echo "✓ 模型供应商初始化完成"
echo "✓ 主流模型初始化完成"
echo "✅ 已添加阿里云百炼供应商和模型"

