WITH seedance20_models AS (
  SELECT *
  FROM (
    VALUES
      (
        'volces:doubao-seedance-2-0-260128',
        'doubao-seedance-2-0-260128',
        '["video_generate","image_to_video","omni_video"]'::jsonb,
        '{
          "video_generate": {
            "output_resolutions": ["720p", "480p", "1080p", "2160p"],
            "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
            "duration_range": [4, 15],
            "input_audio": true,
            "output_audio": true,
            "supportWebSearch": true,
            "output_support_return_last_frame": true,
            "output_video_continuation": true,
            "max_images": 9,
            "max_videos": 3
          },
          "image_to_video": {
            "output_resolutions": ["720p", "480p", "1080p", "2160p"],
            "input_first_frame": true,
            "input_first_last_frame": true,
            "input_last_frame": false,
            "input_reference_generate_single": true,
            "input_reference_generate_multiple": true,
            "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
            "duration_range": [4, 15],
            "input_audio": true,
            "support_video_effect_template": false,
            "output_audio": true,
            "supportWebSearch": true,
            "output_support_return_last_frame": true,
            "output_video_continuation": true,
            "max_images": 9,
            "max_videos": 3,
            "max_images_for_last_frame": 2
          },
          "omni_video": {
            "supported_modes": ["element_reference", "text_to_video", "image_reference", "first_last_frame", "video_reference", "video_edit", "multi_shot"],
            "output_resolutions": ["720p", "480p", "1080p", "2160p"],
            "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
            "duration_options": [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
            "output_audio": true,
            "input_audio": true,
            "supportWebSearch": true,
            "output_support_return_last_frame": true,
            "output_video_continuation": true,
            "max_videos": 3,
            "max_audios": 3,
            "max_images": 9,
            "max_elements": 9,
            "max_images_and_elements": 9,
            "max_images_for_last_frame": 2,
            "support_instruction_edit": true,
            "supports_portrait_asset_reference": true
          },
          "originalTypes": ["video_generate", "image_to_video", "omni_video"]
        }'::jsonb
      ),
      (
        'volces:doubao-seedance-2-0-fast-260128',
        'doubao-seedance-2-0-fast-260128',
        '["video_generate","image_to_video","omni_video"]'::jsonb,
        '{
          "video_generate": {
            "output_resolutions": ["720p", "480p"],
            "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
            "duration_range": [4, 15],
            "input_audio": true,
            "output_audio": true,
            "supportWebSearch": true,
            "output_support_return_last_frame": true,
            "output_video_continuation": true,
            "max_images": 9,
            "max_videos": 3
          },
          "image_to_video": {
            "output_resolutions": ["720p", "480p"],
            "input_first_frame": true,
            "input_first_last_frame": true,
            "input_last_frame": false,
            "input_reference_generate_single": true,
            "input_reference_generate_multiple": true,
            "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
            "duration_range": [4, 15],
            "input_audio": true,
            "support_video_effect_template": false,
            "output_audio": true,
            "supportWebSearch": true,
            "output_support_return_last_frame": true,
            "output_video_continuation": true,
            "max_images": 9,
            "max_videos": 3,
            "max_images_for_last_frame": 2
          },
          "omni_video": {
            "supported_modes": ["element_reference", "text_to_video", "image_reference", "first_last_frame", "video_reference", "video_edit", "multi_shot"],
            "output_resolutions": ["720p", "480p"],
            "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
            "duration_options": [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
            "output_audio": true,
            "input_audio": true,
            "supportWebSearch": true,
            "output_support_return_last_frame": true,
            "output_video_continuation": true,
            "max_videos": 3,
            "max_audios": 3,
            "max_images": 9,
            "max_elements": 9,
            "max_images_and_elements": 9,
            "max_images_for_last_frame": 2,
            "support_instruction_edit": true,
            "supports_portrait_asset_reference": true
          },
          "originalTypes": ["video_generate", "image_to_video", "omni_video"]
        }'::jsonb
      ),
      (
        'volces:doubao-seedance-2-0-mini-260615',
        'doubao-seedance-2-0-mini-260615',
        '["video_generate","image_to_video","omni_video"]'::jsonb,
        '{
          "video_generate": {
            "output_resolutions": ["720p", "480p"],
            "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
            "duration_range": [4, 15],
            "input_audio": true,
            "output_audio": true,
            "supportWebSearch": true,
            "output_support_return_last_frame": true,
            "output_video_continuation": true,
            "max_images": 9,
            "max_videos": 3
          },
          "image_to_video": {
            "output_resolutions": ["720p", "480p"],
            "input_first_frame": true,
            "input_first_last_frame": true,
            "input_last_frame": false,
            "input_reference_generate_single": true,
            "input_reference_generate_multiple": true,
            "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
            "duration_range": [4, 15],
            "input_audio": true,
            "support_video_effect_template": false,
            "output_audio": true,
            "supportWebSearch": true,
            "output_support_return_last_frame": true,
            "output_video_continuation": true,
            "max_images": 9,
            "max_videos": 3,
            "max_images_for_last_frame": 2
          },
          "omni_video": {
            "supported_modes": ["element_reference", "text_to_video", "image_reference", "first_last_frame", "video_reference", "video_edit", "multi_shot"],
            "output_resolutions": ["720p", "480p"],
            "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
            "duration_options": [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
            "output_audio": true,
            "input_audio": true,
            "supportWebSearch": true,
            "output_support_return_last_frame": true,
            "output_video_continuation": true,
            "max_videos": 3,
            "max_audios": 3,
            "max_images": 9,
            "max_elements": 9,
            "max_images_and_elements": 9,
            "max_images_for_last_frame": 2,
            "support_instruction_edit": true,
            "supports_portrait_asset_reference": true
          },
          "originalTypes": ["video_generate", "image_to_video", "omni_video"]
        }'::jsonb
      )
  ) AS item(canonical_model_key, provider_model_name, model_type, capabilities)
),
seedance20_payload AS (
  SELECT
    *,
    '{"max_request_per_minute":600,"max_concurrent_requests":10}'::jsonb AS model_limits,
    '{"rules":[{"metric":"rpm","limit":600,"windowSeconds":60},{"metric":"concurrent","limit":10,"leaseTtlSeconds":120}]}'::jsonb AS rate_limit_policy
  FROM seedance20_models
)
UPDATE base_model_catalog base_model
SET model_type = seedance_model.model_type,
    capabilities = seedance_model.capabilities,
    default_rate_limit_policy = seedance_model.rate_limit_policy,
    metadata = jsonb_set(
      COALESCE(base_model.metadata, '{}'::jsonb),
      '{rawModel}',
      COALESCE(base_model.metadata->'rawModel', '{}'::jsonb)
        || jsonb_build_object(
          'capabilities', seedance_model.capabilities,
          'model_limits', seedance_model.model_limits
        ),
      true
    ),
    default_snapshot = CASE
      WHEN COALESCE(base_model.default_snapshot, '{}'::jsonb) = '{}'::jsonb THEN base_model.default_snapshot
      ELSE jsonb_set(
        jsonb_set(
          jsonb_set(
            jsonb_set(
              base_model.default_snapshot,
              '{modelType}',
              seedance_model.model_type,
              true
            ),
            '{capabilities}',
            seedance_model.capabilities,
            true
          ),
          '{defaultRateLimitPolicy}',
          seedance_model.rate_limit_policy,
          true
        ),
        '{metadata}',
        COALESCE(base_model.default_snapshot->'metadata', '{}'::jsonb)
          || jsonb_build_object(
            'rawModel',
            COALESCE(base_model.default_snapshot->'metadata'->'rawModel', '{}'::jsonb)
              || jsonb_build_object(
                'capabilities', seedance_model.capabilities,
                'model_limits', seedance_model.model_limits
              )
          ),
        true
      )
    END,
    updated_at = now()
FROM seedance20_payload seedance_model
WHERE base_model.provider_key = 'volces'
  AND (
    base_model.canonical_model_key = seedance_model.canonical_model_key
    OR base_model.provider_model_name = seedance_model.provider_model_name
  );

WITH seedance20_models AS (
  SELECT *
  FROM (
    VALUES
      (
        'doubao-seedance-2-0-260128',
        '["video_generate","image_to_video","omni_video"]'::jsonb,
        '{
          "video_generate": {"output_resolutions": ["720p", "480p", "1080p", "2160p"], "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"], "duration_range": [4, 15], "input_audio": true, "output_audio": true, "supportWebSearch": true, "output_support_return_last_frame": true, "output_video_continuation": true, "max_images": 9, "max_videos": 3},
          "image_to_video": {"output_resolutions": ["720p", "480p", "1080p", "2160p"], "input_first_frame": true, "input_first_last_frame": true, "input_last_frame": false, "input_reference_generate_single": true, "input_reference_generate_multiple": true, "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"], "duration_range": [4, 15], "input_audio": true, "support_video_effect_template": false, "output_audio": true, "supportWebSearch": true, "output_support_return_last_frame": true, "output_video_continuation": true, "max_images": 9, "max_videos": 3, "max_images_for_last_frame": 2},
          "omni_video": {"supported_modes": ["element_reference", "text_to_video", "image_reference", "first_last_frame", "video_reference", "video_edit", "multi_shot"], "output_resolutions": ["720p", "480p", "1080p", "2160p"], "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"], "duration_options": [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], "output_audio": true, "input_audio": true, "supportWebSearch": true, "output_support_return_last_frame": true, "output_video_continuation": true, "max_videos": 3, "max_audios": 3, "max_images": 9, "max_elements": 9, "max_images_and_elements": 9, "max_images_for_last_frame": 2, "support_instruction_edit": true, "supports_portrait_asset_reference": true},
          "originalTypes": ["video_generate", "image_to_video", "omni_video"]
        }'::jsonb
      ),
      (
        'doubao-seedance-2-0-fast-260128',
        '["video_generate","image_to_video","omni_video"]'::jsonb,
        '{
          "video_generate": {"output_resolutions": ["720p", "480p"], "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"], "duration_range": [4, 15], "input_audio": true, "output_audio": true, "supportWebSearch": true, "output_support_return_last_frame": true, "output_video_continuation": true, "max_images": 9, "max_videos": 3},
          "image_to_video": {"output_resolutions": ["720p", "480p"], "input_first_frame": true, "input_first_last_frame": true, "input_last_frame": false, "input_reference_generate_single": true, "input_reference_generate_multiple": true, "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"], "duration_range": [4, 15], "input_audio": true, "support_video_effect_template": false, "output_audio": true, "supportWebSearch": true, "output_support_return_last_frame": true, "output_video_continuation": true, "max_images": 9, "max_videos": 3, "max_images_for_last_frame": 2},
          "omni_video": {"supported_modes": ["element_reference", "text_to_video", "image_reference", "first_last_frame", "video_reference", "video_edit", "multi_shot"], "output_resolutions": ["720p", "480p"], "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"], "duration_options": [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], "output_audio": true, "input_audio": true, "supportWebSearch": true, "output_support_return_last_frame": true, "output_video_continuation": true, "max_videos": 3, "max_audios": 3, "max_images": 9, "max_elements": 9, "max_images_and_elements": 9, "max_images_for_last_frame": 2, "support_instruction_edit": true, "supports_portrait_asset_reference": true},
          "originalTypes": ["video_generate", "image_to_video", "omni_video"]
        }'::jsonb
      ),
      (
        'doubao-seedance-2-0-mini-260615',
        '["video_generate","image_to_video","omni_video"]'::jsonb,
        '{
          "video_generate": {"output_resolutions": ["720p", "480p"], "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"], "duration_range": [4, 15], "input_audio": true, "output_audio": true, "supportWebSearch": true, "output_support_return_last_frame": true, "output_video_continuation": true, "max_images": 9, "max_videos": 3},
          "image_to_video": {"output_resolutions": ["720p", "480p"], "input_first_frame": true, "input_first_last_frame": true, "input_last_frame": false, "input_reference_generate_single": true, "input_reference_generate_multiple": true, "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"], "duration_range": [4, 15], "input_audio": true, "support_video_effect_template": false, "output_audio": true, "supportWebSearch": true, "output_support_return_last_frame": true, "output_video_continuation": true, "max_images": 9, "max_videos": 3, "max_images_for_last_frame": 2},
          "omni_video": {"supported_modes": ["element_reference", "text_to_video", "image_reference", "first_last_frame", "video_reference", "video_edit", "multi_shot"], "output_resolutions": ["720p", "480p"], "aspect_ratio_allowed": ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"], "duration_options": [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], "output_audio": true, "input_audio": true, "supportWebSearch": true, "output_support_return_last_frame": true, "output_video_continuation": true, "max_videos": 3, "max_audios": 3, "max_images": 9, "max_elements": 9, "max_images_and_elements": 9, "max_images_for_last_frame": 2, "support_instruction_edit": true, "supports_portrait_asset_reference": true},
          "originalTypes": ["video_generate", "image_to_video", "omni_video"]
        }'::jsonb
      )
  ) AS item(provider_model_name, model_type, capabilities)
),
seedance20_payload AS (
  SELECT
    *,
    '{"rules":[{"metric":"rpm","limit":600,"windowSeconds":60},{"metric":"concurrent","limit":10,"leaseTtlSeconds":120}]}'::jsonb AS rate_limit_policy
  FROM seedance20_models
)
UPDATE platform_models model
SET model_type = seedance_model.model_type,
    capabilities = seedance_model.capabilities,
    rate_limit_policy = seedance_model.rate_limit_policy,
    updated_at = now()
FROM integration_platforms platform
JOIN seedance20_payload seedance_model ON TRUE
WHERE model.platform_id = platform.id
  AND platform.provider = 'volces'
  AND platform.deleted_at IS NULL
  AND COALESCE(NULLIF(model.provider_model_name, ''), model.model_name) = seedance_model.provider_model_name;
