WITH seedream50_pro AS (
  SELECT
    'volces:doubao-seedream-5-0-pro-260628' AS canonical_model_key,
    'doubao-seedream-5-0-pro-260628' AS provider_model_name,
    'Seedream-5.0-Pro' AS model_alias,
    'Seedream 5.0 Pro' AS display_name,
    '["image_edit","image_generate"]'::jsonb AS model_type,
    '{
      "image_edit": {
        "input_multiple_images": true,
        "input_max_images_count": 10,
        "input_max_file_size_bytes": 31457280,
        "output_resolutions": ["1K", "2K"],
        "size_param_format": "resolution",
        "allow_custom_width_height_size": true,
        "output_max_size": 4624220,
        "output_size_range": [921600, 4624220],
        "aspect_ratio_range": [0.0625, 16],
        "output_multiple_images": false,
        "output_max_images_count": 1
      },
      "image_generate": {
        "output_resolutions": ["1K", "2K"],
        "size_param_format": "resolution",
        "allow_custom_width_height_size": true,
        "output_max_size": 4624220,
        "output_size_range": [921600, 4624220],
        "aspect_ratio_range": [0.0625, 16],
        "output_multiple_images": false,
        "output_max_images_count": 1
      },
      "stream": false,
      "supportWebSearch": false,
      "originalTypes": ["image_edit", "image_generate"]
    }'::jsonb AS capabilities,
    '{
      "image": {
        "basePrice": 10,
        "baseWeight": 1,
        "dynamicWeight": {"1K": 1, "2K": 1.5, "3K": 1.75, "4K": 2, "8K": 4}
      },
      "currency": "resource"
    }'::jsonb AS base_billing_config,
    '{"rules":[{"metric":"rpm","limit":500,"windowSeconds":60}]}'::jsonb AS rate_limit_policy,
    '{
      "source": "server-main.integration-platform",
      "sourceProviderCode": "volces",
      "sourceProviderName": "火山引擎（豆包）",
      "sourceSpecType": "volces",
      "originalTypes": ["image_edit", "image_generate"],
      "alias": "Seedream-5.0-Pro",
      "displayName": "Seedream 5.0 Pro",
      "description": "高精度图片生成与多参考图编辑，支持 1K、2K 和自定义尺寸",
      "iconPath": "https://ecmb.bdimg.com/tam-ogel/1801637412_-164881388_88_88.jpg",
      "billingType": "external-api",
      "billingMode": "",
      "referenceModel": "",
      "modelWeight": null,
      "selectable": true,
      "rawModel": {
        "name": "doubao-seedream-5-0-pro-260628",
        "types": ["image_edit", "image_generate"],
        "alias": "Seedream-5.0-Pro",
        "display_name": "Seedream 5.0 Pro",
        "description": "高精度图片生成与多参考图编辑，支持 1K、2K 和自定义尺寸",
        "icon_path": "https://ecmb.bdimg.com/tam-ogel/1801637412_-164881388_88_88.jpg",
        "model_limits": {"max_request_per_minute": 500}
      }
    }'::jsonb AS metadata
)
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,
  metadata,
  catalog_type,
  default_snapshot,
  status
)
SELECT
  (SELECT id FROM model_catalog_providers WHERE provider_key = 'volces' OR provider_code = 'volces' LIMIT 1),
  'volces',
  model.canonical_model_key,
  model.provider_model_name,
  model.model_type,
  model.model_alias,
  model.capabilities,
  model.base_billing_config,
  model.rate_limit_policy,
  jsonb_set(model.metadata, '{rawModel,capabilities}', model.capabilities, true),
  'system',
  jsonb_build_object(
    'providerKey', 'volces',
    'canonicalModelKey', model.canonical_model_key,
    'providerModelName', model.provider_model_name,
    'modelType', model.model_type,
    'modelAlias', model.model_alias,
    'displayName', model.display_name,
    'capabilities', model.capabilities,
    'baseBillingConfig', model.base_billing_config,
    'defaultRateLimitPolicy', model.rate_limit_policy,
    'metadata', jsonb_set(model.metadata, '{rawModel,capabilities}', model.capabilities, true),
    'status', 'active'
  ),
  'active'
FROM seedream50_pro model
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,
    metadata = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.metadata ELSE base_model_catalog.metadata END,
    catalog_type = CASE WHEN base_model_catalog.customized_at IS NULL THEN 'system' ELSE base_model_catalog.catalog_type END,
    default_snapshot = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.default_snapshot ELSE base_model_catalog.default_snapshot END,
    status = CASE WHEN base_model_catalog.customized_at IS NULL THEN 'active' ELSE base_model_catalog.status END,
    updated_at = now();

WITH seedream50_pro AS (
  SELECT
    base_model.id AS base_model_id,
    base_model.provider_model_name,
    'Seedream-5.0-Pro' AS model_alias,
    'Seedream 5.0 Pro' AS display_name,
    base_model.model_type,
    base_model.capabilities,
    base_model.base_billing_config,
    base_model.default_rate_limit_policy
  FROM base_model_catalog base_model
  WHERE base_model.canonical_model_key = 'volces:doubao-seedream-5-0-pro-260628'
)
UPDATE platform_models model
SET base_model_id = seedream.base_model_id,
    model_name = CASE
      WHEN model.model_name = seedream.model_alias THEN seedream.model_alias
      WHEN NOT EXISTS (
        SELECT 1
        FROM platform_models peer
        WHERE peer.platform_id = model.platform_id
          AND peer.id <> model.id
          AND peer.model_name = seedream.model_alias
      ) THEN seedream.model_alias
      ELSE model.model_name
    END,
    provider_model_name = seedream.provider_model_name,
    model_alias = seedream.model_alias,
    model_type = seedream.model_type,
    display_name = seedream.display_name,
    capabilities = seedream.capabilities,
    pricing_mode = 'inherit_discount',
    billing_config = seedream.base_billing_config,
    retry_policy = '{"enabled":true,"maxAttempts":1}'::jsonb,
    rate_limit_policy = seedream.default_rate_limit_policy,
    enabled = NOT EXISTS (
      SELECT 1
      FROM platform_models peer
      WHERE peer.platform_id = model.platform_id
        AND peer.id <> model.id
        AND peer.model_name = seedream.model_alias
    ),
    updated_at = now()
FROM integration_platforms platform
JOIN seedream50_pro seedream ON TRUE
WHERE model.platform_id = platform.id
  AND platform.provider = 'volces'
  AND platform.deleted_at IS NULL
  AND (
    model.model_name IN ('Seedream-5.0-Pro', 'doubao-seedream-5-0-pro-260628')
    OR model.provider_model_name = 'doubao-seedream-5-0-pro-260628'
    OR model.model_alias = 'Seedream-5.0-Pro'
  );

INSERT INTO platform_models (
  platform_id,
  base_model_id,
  model_name,
  provider_model_name,
  model_alias,
  model_type,
  display_name,
  capabilities,
  pricing_mode,
  billing_config,
  retry_policy,
  rate_limit_policy,
  enabled
)
SELECT
  platform.id,
  base_model.id,
  'Seedream-5.0-Pro',
  base_model.provider_model_name,
  'Seedream-5.0-Pro',
  base_model.model_type,
  'Seedream 5.0 Pro',
  base_model.capabilities,
  'inherit_discount',
  base_model.base_billing_config,
  '{"enabled":true,"maxAttempts":1}'::jsonb,
  base_model.default_rate_limit_policy,
  true
FROM integration_platforms platform
JOIN base_model_catalog base_model ON base_model.canonical_model_key = 'volces:doubao-seedream-5-0-pro-260628'
WHERE platform.provider = 'volces'
  AND platform.deleted_at IS NULL
  AND NOT EXISTS (
    SELECT 1
    FROM platform_models existing
    WHERE existing.platform_id = platform.id
      AND (
        existing.model_name IN ('Seedream-5.0-Pro', 'doubao-seedream-5-0-pro-260628')
        OR existing.provider_model_name = 'doubao-seedream-5-0-pro-260628'
        OR existing.model_alias = 'Seedream-5.0-Pro'
      )
  )
ON CONFLICT (platform_id, model_name) DO UPDATE
SET base_model_id = EXCLUDED.base_model_id,
    provider_model_name = EXCLUDED.provider_model_name,
    model_alias = EXCLUDED.model_alias,
    display_name = EXCLUDED.display_name,
    model_type = EXCLUDED.model_type,
    capabilities = EXCLUDED.capabilities,
    pricing_mode = EXCLUDED.pricing_mode,
    billing_config = EXCLUDED.billing_config,
    retry_policy = EXCLUDED.retry_policy,
    rate_limit_policy = EXCLUDED.rate_limit_policy,
    enabled = EXCLUDED.enabled,
    updated_at = now();
