WITH aliyun_model_defs AS (
  SELECT
    'aliyun-bailian-openai' AS provider_key,
    'aliyun-bailian-openai:text-embedding-v4' AS canonical_model_key,
    'text-embedding-v4' AS provider_model_name,
    'Qwen3-Embedding-v4' AS display_name,
    'Qwen3-Embedding-v4' AS model_alias,
    'Qwen3-Embedding 系列，默认维度 1024，最长输入 8192 tokens；支持 100+ 语种与多种编程语言。' AS description,
    'https://static.51easyai.com/qwen-color.webp' AS icon_path,
    jsonb_build_array('text_embedding') AS model_type,
    jsonb_build_object(
      'text_embedding', jsonb_build_object(
        'dimensions', jsonb_build_array(2048, 1536, 1024, 768, 512, 256, 128, 64),
        'defaultDimension', 1024,
        'maxRows', 10,
        'maxTokensPerRow', 8192
      ),
      'originalTypes', jsonb_build_array('text_embedding')
    ) AS capabilities
  UNION ALL
  SELECT
    'aliyun-bailian-openai',
    'aliyun-bailian-openai:text-embedding-v3',
    'text-embedding-v3',
    'Qwen3-Embedding-v3',
    'Qwen3-Embedding-v3',
    'Qwen3-Embedding 系列，默认维度 1024；支持中文、英文及 50+ 主流语种。',
    'https://static.51easyai.com/qwen-color.webp',
    jsonb_build_array('text_embedding'),
    jsonb_build_object(
      'text_embedding', jsonb_build_object(
        'dimensions', jsonb_build_array(1024, 768, 512, 256, 128, 64),
        'defaultDimension', 1024
      ),
      'originalTypes', jsonb_build_array('text_embedding')
    )
  UNION ALL
  SELECT
    'aliyun-bailian-openai',
    'aliyun-bailian-openai:text-embedding-v2',
    'text-embedding-v2',
    'Text-Embedding-v2',
    'Text-Embedding-v2',
    '固定维度 1536，最长输入 2048 tokens；支持中英西法葡印尼日韩德俄等语种。',
    'https://static.51easyai.com/qwen-color.webp',
    jsonb_build_array('text_embedding'),
    jsonb_build_object(
      'text_embedding', jsonb_build_object(
        'dimensions', jsonb_build_array(1536),
        'defaultDimension', 1536,
        'maxRows', 25,
        'maxTokensPerRow', 2048
      ),
      'originalTypes', jsonb_build_array('text_embedding')
    )
  UNION ALL
  SELECT
    'aliyun-bailian-openai',
    'aliyun-bailian-openai:text-embedding-v1',
    'text-embedding-v1',
    'Text-Embedding-v1',
    'Text-Embedding-v1',
    '百炼 OpenAI 兼容渠道 embedding 基础模型。',
    'https://static.51easyai.com/qwen-color.webp',
    jsonb_build_array('text_embedding'),
    jsonb_build_object('originalTypes', jsonb_build_array('text_embedding'))
  UNION ALL
  SELECT
    'aliyun-bailian-openai',
    'aliyun-bailian-openai:qwen3-rerank',
    'qwen3-rerank',
    'Qwen3-Rerank',
    'Qwen3-Rerank',
    '阿里云百炼 OpenAI 兼容重排序模型，支持 100+ 语种，适用于语义文本搜索和 RAG。',
    'https://static.51easyai.com/qwen-color.webp',
    jsonb_build_array('text_rerank'),
    jsonb_build_object(
      'text_rerank', jsonb_build_object(
        'maxDocuments', 500,
        'maxTokensPerDocument', 4000,
        'maxRequestTokens', 120000,
        'supportTopN', true,
        'supportInstruct', true
      ),
      'originalTypes', jsonb_build_array('text_rerank')
    )
),
source_rows AS (
  SELECT
    providers.id AS provider_id,
    defs.provider_key,
    defs.canonical_model_key,
    defs.provider_model_name,
    defs.model_type,
    defs.display_name,
    defs.model_alias,
    defs.description,
    defs.icon_path,
    defs.capabilities,
    COALESCE(template.base_billing_config, '{"text":{"basePrice":0.01,"baseWeight":1}}'::jsonb) AS base_billing_config,
    COALESCE(template.default_rate_limit_policy, '{}'::jsonb) AS default_rate_limit_policy,
    COALESCE(
      template.pricing_rule_set_id,
      (SELECT id FROM model_pricing_rule_sets WHERE rule_set_key = 'default-multimodal-v1' LIMIT 1)
    ) AS pricing_rule_set_id,
    COALESCE(
      template.runtime_policy_set_id,
      (SELECT id FROM model_runtime_policy_sets WHERE policy_key = 'default-runtime-v1' LIMIT 1)
    ) AS runtime_policy_set_id,
    COALESCE(template.runtime_policy_override, '{}'::jsonb) AS runtime_policy_override,
    COALESCE(template.pricing_version, 1) AS pricing_version
  FROM aliyun_model_defs defs
  LEFT JOIN model_catalog_providers providers
    ON providers.provider_key = defs.provider_key
    OR providers.provider_code = defs.provider_key
  LEFT JOIN base_model_catalog template
    ON template.canonical_model_key = 'aliyun-bailian-openai:text-embedding-v4'
),
payload AS (
  SELECT
    source_rows.*,
    jsonb_build_object(
      'source', 'aliyun.model-studio.docs',
      'sourceProviderCode', provider_key,
      'sourceProviderName', '阿里云百炼(OpenAI兼容）',
      'sourceSpecType', 'openai',
      'originalTypes', model_type,
      'alias', model_alias,
      'description', description,
      'iconPath', icon_path,
      'billingType', 'external-api',
      'billingMode', '',
      'referenceModel', '',
      'modelWeight', NULL,
      'selectable', true,
      'rawModel', jsonb_build_object(
        'name', provider_model_name,
        'types', model_type,
        'icon_path', icon_path,
        'alias', model_alias,
        'description', description,
        'capabilities', capabilities - 'originalTypes'
      )
    ) AS metadata
  FROM source_rows
),
snapshot AS (
  SELECT
    payload.*,
    jsonb_build_object(
      'providerKey', provider_key,
      'canonicalModelKey', canonical_model_key,
      'providerModelName', provider_model_name,
      'modelType', model_type,
      'modelAlias', model_alias,
      'capabilities', capabilities,
      'baseBillingConfig', base_billing_config,
      'defaultRateLimitPolicy', default_rate_limit_policy,
      'pricingRuleSetId', COALESCE(pricing_rule_set_id::text, ''),
      'runtimePolicySetId', COALESCE(runtime_policy_set_id::text, ''),
      'runtimePolicyOverride', runtime_policy_override,
      'metadata', metadata,
      'pricingVersion', pricing_version,
      'status', 'active'
    ) AS default_snapshot
  FROM payload
)
INSERT INTO base_model_catalog (
  provider_id,
  provider_key,
  canonical_model_key,
  provider_model_name,
  model_type,
  display_name,
  capabilities,
  base_billing_config,
  default_rate_limit_policy,
  pricing_rule_set_id,
  runtime_policy_set_id,
  runtime_policy_override,
  metadata,
  catalog_type,
  default_snapshot,
  pricing_version,
  status
)
SELECT
  provider_id,
  provider_key,
  canonical_model_key,
  provider_model_name,
  model_type,
  display_name,
  capabilities,
  base_billing_config,
  default_rate_limit_policy,
  pricing_rule_set_id,
  runtime_policy_set_id,
  runtime_policy_override,
  metadata,
  'system',
  default_snapshot,
  pricing_version,
  'active'
FROM snapshot
ON CONFLICT (canonical_model_key) DO UPDATE
SET provider_id = EXCLUDED.provider_id,
    provider_key = EXCLUDED.provider_key,
    provider_model_name = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.provider_model_name
      ELSE base_model_catalog.provider_model_name
    END,
    model_type = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.model_type
      ELSE base_model_catalog.model_type
    END,
    display_name = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.display_name
      ELSE base_model_catalog.display_name
    END,
    capabilities = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.capabilities
      ELSE base_model_catalog.capabilities
    END,
    base_billing_config = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.base_billing_config
      ELSE base_model_catalog.base_billing_config
    END,
    default_rate_limit_policy = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.default_rate_limit_policy
      ELSE base_model_catalog.default_rate_limit_policy
    END,
    pricing_rule_set_id = COALESCE(base_model_catalog.pricing_rule_set_id, EXCLUDED.pricing_rule_set_id),
    runtime_policy_set_id = COALESCE(base_model_catalog.runtime_policy_set_id, EXCLUDED.runtime_policy_set_id),
    runtime_policy_override = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.runtime_policy_override
      ELSE base_model_catalog.runtime_policy_override
    END,
    metadata = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.metadata
      ELSE base_model_catalog.metadata
    END,
    catalog_type = 'system',
    default_snapshot = EXCLUDED.default_snapshot,
    pricing_version = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.pricing_version
      ELSE base_model_catalog.pricing_version
    END,
    status = CASE
      WHEN base_model_catalog.customized_at IS NULL THEN 'active'
      ELSE base_model_catalog.status
    END,
    updated_at = now();
