WITH qwen_capabilities AS (
  SELECT
    jsonb_build_object(
      'text_generate', jsonb_build_object(
        'supportTool', true,
        'supportStructuredOutput', true,
        'supportThinking', true,
        'supportWebSearch', true,
        'max_context_tokens', 262144
      ),
      'image_analysis', jsonb_build_object(
        'supportStructuredOutput', true,
        'max_context_tokens', 262144
      ),
      'video_understanding', jsonb_build_object(
        'supportStructuredOutput', true,
        'max_context_tokens', 262144
      ),
      'tools_call', jsonb_build_object(
        'supportTool', true,
        'supportStructuredOutput', true,
        'supportThinking', true,
        'supportWebSearch', true,
        'max_context_tokens', 262144
      ),
      'originalTypes', jsonb_build_array(
        'text_generate',
        'image_analysis',
        'video_understanding',
        'tools_call'
      )
    ) AS qwen36_plus,
    jsonb_build_object(
      'text_generate', jsonb_build_object(
        'supportTool', true,
        'supportStructuredOutput', false,
        'supportThinking', true,
        'supportWebSearch', true,
        'max_context_tokens', 262144
      ),
      'tools_call', jsonb_build_object(
        'supportTool', true,
        'supportThinking', true,
        'supportWebSearch', true,
        'max_context_tokens', 262144
      ),
      'originalTypes', jsonb_build_array('text_generate', 'tools_call')
    ) AS qwen37_max
),
qwen_defs AS (
  SELECT
    'easyai' AS provider_key,
    'easyai' AS source_provider_code,
    'EasyAI' AS source_provider_name,
    'easyai' AS source_spec_type,
    'easyai:Qwen3.6-Plus' AS canonical_model_key,
    'Qwen3.6-Plus' AS provider_model_name,
    'Qwen3.6-Plus' AS raw_model_name,
    'Qwen3.6-Plus' AS display_name,
    'Qwen3.6-Plus' AS model_alias,
    'Qwen3.6 原生视觉语言系列 Plus 模型，展现出与当前顶尖前沿模型相媲美的卓越性能，模型效果相较 3.5 系列显著提升。模型在 Agentic coding、前端编程、Vibe coding 等代码能力、多模态万物识别、OCR、物体定位等能力上显著增强。该模型版本功能等同于快照模型 qwen3.6-plus-2026-04-02。' AS description,
    'https://static.51easyai.com/qwen-color.webp' AS icon_path,
    jsonb_build_array(
      'text_generate',
      'image_analysis',
      'video_understanding',
      'tools_call'
    ) AS model_type,
    qwen36_plus AS capabilities,
    'easyai:Qwen3.6-Plus' AS template_key
  FROM qwen_capabilities
  UNION ALL
  SELECT
    'aliyun-bailian-openai',
    'aliyun-bailian-openai',
    '阿里云百炼(OpenAI兼容）',
    'openai',
    'aliyun-bailian-openai:qwen3.6-plus',
    'qwen3.6-plus',
    'qwen3.6-plus',
    'Qwen3.6-Plus',
    'Qwen3.6-Plus',
    'Qwen3.6 原生视觉语言系列 Plus 模型，展现出与当前顶尖前沿模型相媲美的卓越性能，模型效果相较 3.5 系列显著提升。模型在 Agentic coding、前端编程、Vibe coding 等代码能力、多模态万物识别、OCR、物体定位等能力上显著增强。该模型版本功能等同于快照模型 qwen3.6-plus-2026-04-02。',
    'https://static.51easyai.com/qwen-color.webp',
    jsonb_build_array(
      'text_generate',
      'image_analysis',
      'video_understanding',
      'tools_call'
    ),
    qwen36_plus,
    'aliyun-bailian-openai:qwen3.6-plus'
  FROM qwen_capabilities
  UNION ALL
  SELECT
    'easyai',
    'easyai',
    'EasyAI',
    'easyai',
    'easyai:Qwen3.7-Max',
    'Qwen3.7-Max',
    'Qwen3.7-Max',
    'Qwen3.7-Max',
    'Qwen3.7-Max',
    'Qwen3.7 系列中规模最大、综合能力最强的 Max 模型，当前开放纯文本模型能力供体验。Qwen3.7 是面向智能体时代的新一代旗舰模型，核心优势在于智能体能力的广度与深度：在编程、办公与生产力、长期自主执行方面均能出色胜任各项任务。该模型版本功能等同于快照模型 qwen3.7-max-2026-05-20。',
    'https://static.51easyai.com/qwen-color.webp',
    jsonb_build_array('text_generate', 'tools_call'),
    qwen37_max,
    'easyai:Qwen3.6-Plus'
  FROM qwen_capabilities
  UNION ALL
  SELECT
    'aliyun-bailian-openai',
    'aliyun-bailian-openai',
    '阿里云百炼(OpenAI兼容）',
    'openai',
    'aliyun-bailian-openai:qwen3.7-max',
    'qwen3.7-max',
    'qwen3.7-max',
    'Qwen3.7-Max',
    'Qwen3.7-Max',
    'Qwen3.7 系列中规模最大、综合能力最强的 Max 模型，当前开放纯文本模型能力供体验。Qwen3.7 是面向智能体时代的新一代旗舰模型，核心优势在于智能体能力的广度与深度：在编程、办公与生产力、长期自主执行方面均能出色胜任各项任务。该模型版本功能等同于快照模型 qwen3.7-max-2026-05-20。',
    'https://static.51easyai.com/qwen-color.webp',
    jsonb_build_array('text_generate', 'tools_call'),
    qwen37_max,
    'aliyun-bailian-openai:qwen3.6-plus'
  FROM qwen_capabilities
),
source_rows AS (
  SELECT
    providers.id AS provider_id,
    qwen_defs.provider_key,
    qwen_defs.canonical_model_key,
    qwen_defs.provider_model_name,
    qwen_defs.model_type,
    qwen_defs.display_name,
    qwen_defs.capabilities,
    COALESCE(template.base_billing_config, '{}'::jsonb) AS base_billing_config,
    COALESCE(
      template.default_rate_limit_policy,
      jsonb_build_object(
        'platformLimits',
        jsonb_build_object('max_concurrent_requests', 5)
      )
    ) 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,
    jsonb_build_object(
      'source', 'server-main.integration-platform',
      'sourceProviderCode', qwen_defs.source_provider_code,
      'sourceProviderName', qwen_defs.source_provider_name,
      'sourceSpecType', qwen_defs.source_spec_type,
      'originalTypes', qwen_defs.model_type,
      'alias', qwen_defs.model_alias,
      'description', qwen_defs.description,
      'iconPath', qwen_defs.icon_path,
      'billingType', 'external-api',
      'billingMode', '',
      'referenceModel', '',
      'modelWeight', NULL,
      'selectable', true,
      'rawModel', jsonb_build_object(
        'name', qwen_defs.raw_model_name,
        'types', qwen_defs.model_type,
        'icon_path', qwen_defs.icon_path,
        'alias', qwen_defs.model_alias,
        'description', qwen_defs.description,
        'capabilities', qwen_defs.capabilities - 'originalTypes'
      )
    ) AS metadata
  FROM qwen_defs
  LEFT JOIN model_catalog_providers providers
    ON providers.provider_key = qwen_defs.provider_key
    OR providers.provider_code = qwen_defs.provider_key
  LEFT JOIN base_model_catalog template
    ON template.canonical_model_key = qwen_defs.template_key
),
payload AS (
  SELECT
    source_rows.*,
    jsonb_build_object(
      'providerKey', source_rows.provider_key,
      'canonicalModelKey', source_rows.canonical_model_key,
      'providerModelName', source_rows.provider_model_name,
      'modelType', source_rows.model_type,
      'modelAlias', source_rows.display_name,
      'capabilities', source_rows.capabilities,
      'baseBillingConfig', source_rows.base_billing_config,
      'defaultRateLimitPolicy', source_rows.default_rate_limit_policy,
      'pricingRuleSetId', COALESCE(source_rows.pricing_rule_set_id::text, ''),
      'runtimePolicySetId', COALESCE(source_rows.runtime_policy_set_id::text, ''),
      'runtimePolicyOverride', source_rows.runtime_policy_override,
      'metadata', source_rows.metadata,
      'pricingVersion', source_rows.pricing_version,
      'status', 'active'
    ) AS default_snapshot
  FROM source_rows
)
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 payload
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();
