-- Tencent Seedance SE accepts input images only within the observed provider
-- limits. The runtime uses these capability fields to auto-convert images
-- before forwarding the request.
WITH input_constraints AS (
  SELECT jsonb_build_object(
    'input_image_resolution_range',
    '{"min":{"long_edge":360,"short_edge":360},"max":{"long_edge":1920,"short_edge":1080}}'::jsonb,
    'input_image_aspect_ratio_range',
    '[0.39,2.5]'::jsonb
  ) AS value
),
target_base_models AS (
  SELECT
    base_model.id,
    COALESCE(base_model.capabilities, '{}'::jsonb) || jsonb_build_object(
      'image_to_video',
      COALESCE(base_model.capabilities->'image_to_video', '{}'::jsonb) || input_constraints.value,
      'omni_video',
      COALESCE(base_model.capabilities->'omni_video', '{}'::jsonb) || input_constraints.value
    ) AS image_capabilities
  FROM base_model_catalog base_model
  CROSS JOIN input_constraints
  WHERE base_model.canonical_model_key IN (
      'easyai:豆包Seedance-2.0',
      'easyai:豆包Seedance-2.0-fast'
    )
    OR (
      base_model.provider_key = 'easyai'
      AND base_model.provider_model_name IN ('豆包Seedance-2.0', '豆包Seedance-2.0-fast')
    )
    OR (
      base_model.provider_key = 'tencent-hunyuan-video'
      AND lower(base_model.provider_model_name) IN ('2.0', '2.0-fast')
    )
),
updated_base_models AS (
  UPDATE base_model_catalog base_model
  SET capabilities = target.image_capabilities,
      metadata = jsonb_set(
        COALESCE(base_model.metadata, '{}'::jsonb) || jsonb_build_object(
          'rawModel',
          COALESCE(base_model.metadata->'rawModel', '{}'::jsonb)
        ),
        '{rawModel,capabilities}',
        target.image_capabilities - 'originalTypes',
        true
      ),
      default_snapshot = CASE
        WHEN COALESCE(base_model.default_snapshot, '{}'::jsonb) = '{}'::jsonb
          THEN base_model.default_snapshot
        ELSE jsonb_set(
          jsonb_set(
            base_model.default_snapshot || jsonb_build_object(
              'metadata',
              COALESCE(base_model.default_snapshot->'metadata', '{}'::jsonb) || jsonb_build_object(
                'rawModel',
                COALESCE(base_model.default_snapshot#>'{metadata,rawModel}', '{}'::jsonb)
              )
            ),
            '{capabilities}',
            target.image_capabilities,
            true
          ),
          '{metadata,rawModel,capabilities}',
          target.image_capabilities - 'originalTypes',
          true
        )
      END,
      updated_at = now()
  FROM target_base_models target
  WHERE base_model.id = target.id
  RETURNING base_model.id
)
UPDATE platform_models platform_model
SET capabilities = COALESCE(platform_model.capabilities, '{}'::jsonb) || jsonb_build_object(
      'image_to_video',
      COALESCE(platform_model.capabilities->'image_to_video', '{}'::jsonb) || input_constraints.value,
      'omni_video',
      COALESCE(platform_model.capabilities->'omni_video', '{}'::jsonb) || input_constraints.value
    ),
    updated_at = now()
FROM integration_platforms platform
CROSS JOIN input_constraints
WHERE platform_model.platform_id = platform.id
  AND platform.deleted_at IS NULL
  AND (
    platform_model.base_model_id IN (SELECT id FROM updated_base_models)
    OR (
      platform.provider = 'easyai'
      AND COALESCE(NULLIF(platform_model.provider_model_name, ''), platform_model.model_name)
        IN ('豆包Seedance-2.0', '豆包Seedance-2.0-fast')
    )
    OR (
      platform.provider = 'tencent-hunyuan-video'
      AND lower(COALESCE(NULLIF(platform_model.provider_model_name, ''), platform_model.model_name))
        IN ('2.0', '2.0-fast')
    )
  );
