WITH keling_omni_models(provider_model_name, model_type, capabilities) AS (
  VALUES
    (
      'kling-video-o1',
      '["video_generate","image_to_video","omni_video"]'::jsonb,
      '{
        "video_generate": {
          "output_resolutions": ["720p", "1080p"],
          "aspect_ratio_allowed": ["16:9", "1:1", "9:16"],
          "duration_options": [3, 4, 5, 6, 7, 8, 9, 10],
          "output_audio": false,
          "input_audio": false,
          "prompt_length_limit": {
            "max": 2500,
            "count_mode": "non_ascii_weighted",
            "label": "可灵口径"
          }
        },
        "image_to_video": {
          "output_resolutions": ["720p", "1080p"],
          "aspect_ratio_allowed": ["16:9", "1:1", "9:16"],
          "duration_options": [3, 4, 5, 6, 7, 8, 9, 10],
          "input_first_frame": true,
          "input_last_frame": false,
          "input_first_last_frame": true,
          "input_reference_generate_single": true,
          "input_reference_generate_multiple": true,
          "max_images": 7,
          "max_images_for_last_frame": 2,
          "support_video_effect_template": false,
          "output_audio": false,
          "input_audio": false,
          "prompt_length_limit": {
            "max": 2500,
            "count_mode": "non_ascii_weighted",
            "label": "可灵口径"
          }
        },
        "omni_video": {
          "supported_modes": ["text_to_video", "image_reference", "element_reference", "first_last_frame", "video_reference", "video_edit"],
          "output_resolutions": ["720p", "1080p"],
          "aspect_ratio_allowed": ["16:9", "1:1", "9:16"],
          "duration_options": [3, 4, 5, 6, 7, 8, 9, 10],
          "output_audio": false,
          "input_audio": false,
          "max_videos": 1,
          "max_audios": 0,
          "max_images": 7,
          "max_elements": 7,
          "max_images_and_elements": 7,
          "limits_with_video": {"max_images_and_elements": 4},
          "max_images_for_last_frame": 2,
          "support_instruction_edit": true,
          "prompt_length_limit": {
            "max": 2500,
            "count_mode": "non_ascii_weighted",
            "label": "可灵口径"
          }
        },
        "originalTypes": ["video_generate", "image_to_video", "omni_video"]
      }'::jsonb
    ),
    (
      'kling-v3-omni',
      '["video_generate","image_to_video","omni_video"]'::jsonb,
      '{
        "video_generate": {
          "output_resolutions": ["720p", "1080p", "2160p"],
          "aspect_ratio_allowed": ["16:9", "1:1", "9:16"],
          "duration_options": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
          "output_audio": true,
          "input_audio": false,
          "prompt_length_limit": {
            "max": 2500,
            "count_mode": "non_ascii_weighted",
            "label": "可灵口径"
          }
        },
        "image_to_video": {
          "output_resolutions": ["720p", "1080p", "2160p"],
          "aspect_ratio_allowed": ["16:9", "1:1", "9:16"],
          "duration_options": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
          "input_first_frame": true,
          "input_last_frame": false,
          "input_first_last_frame": true,
          "input_reference_generate_single": true,
          "input_reference_generate_multiple": true,
          "max_images": 7,
          "max_images_for_last_frame": 2,
          "support_video_effect_template": false,
          "output_audio": true,
          "input_audio": false,
          "prompt_length_limit": {
            "max": 2500,
            "count_mode": "non_ascii_weighted",
            "label": "可灵口径"
          }
        },
        "omni_video": {
          "supported_modes": ["text_to_video", "image_reference", "element_reference", "first_last_frame", "video_reference", "video_edit", "multi_shot"],
          "output_resolutions": ["720p", "1080p", "2160p"],
          "aspect_ratio_allowed": ["16:9", "1:1", "9:16"],
          "duration_options": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
          "output_audio": true,
          "input_audio": false,
          "max_videos": 1,
          "max_audios": 0,
          "max_images": 7,
          "max_elements": 7,
          "max_images_and_elements": 7,
          "limits_with_video": {
            "max_images_and_elements": 4,
            "duration_options": [3, 4, 5, 6, 7, 8, 9, 10]
          },
          "max_images_for_last_frame": 2,
          "support_instruction_edit": true,
          "prompt_length_limit": {
            "max": 2500,
            "count_mode": "non_ascii_weighted",
            "label": "可灵口径"
          }
        },
        "originalTypes": ["video_generate", "image_to_video", "omni_video"]
      }'::jsonb
    )
)
UPDATE base_model_catalog model
SET model_type = defs.model_type,
    capabilities = defs.capabilities,
    metadata = COALESCE(model.metadata, '{}'::jsonb)
      || jsonb_build_object(
        'originalTypes', defs.model_type,
        'rawModel', COALESCE(model.metadata->'rawModel', '{}'::jsonb)
          || jsonb_build_object(
            'types', defs.model_type,
            'capabilities', defs.capabilities
          )
      ),
    default_snapshot = CASE
      WHEN COALESCE(model.default_snapshot, '{}'::jsonb) = '{}'::jsonb THEN model.default_snapshot
      ELSE jsonb_set(
        jsonb_set(
          jsonb_set(model.default_snapshot, '{modelType}', defs.model_type, true),
          '{capabilities}',
          defs.capabilities,
          true
        ),
        '{metadata}',
        COALESCE(model.default_snapshot->'metadata', '{}'::jsonb)
          || jsonb_build_object(
            'originalTypes', defs.model_type,
            'rawModel', COALESCE(model.default_snapshot->'metadata'->'rawModel', '{}'::jsonb)
              || jsonb_build_object(
                'types', defs.model_type,
                'capabilities', defs.capabilities
              )
          ),
        true
      )
    END,
    updated_at = now()
FROM keling_omni_models defs
WHERE model.provider_model_name = defs.provider_model_name
  AND model.model_type @> '["omni_video"]'::jsonb;

WITH keling_omni_models(provider_model_name, model_type, capabilities) AS (
  SELECT DISTINCT ON (model.provider_model_name)
    model.provider_model_name,
    model.model_type,
    model.capabilities
  FROM base_model_catalog model
  WHERE model.provider_model_name IN ('kling-video-o1', 'kling-v3-omni')
    AND model.model_type @> '["omni_video"]'::jsonb
  ORDER BY model.provider_model_name, (model.provider_key = 'keling') DESC, model.created_at ASC
)
UPDATE platform_models model
SET model_type = defs.model_type,
    capabilities = defs.capabilities,
    updated_at = now()
FROM keling_omni_models defs
WHERE COALESCE(NULLIF(model.provider_model_name, ''), model.model_name) = defs.provider_model_name
  AND model.model_type @> '["omni_video"]'::jsonb;
