-- easyai:migration:reapply-after-acceptance-import
-- A production snapshot is imported after the local schema is migrated. Replay
-- this idempotent catalog reconciliation so the local target state includes the
-- data changes delivered by the release under test.
WITH input_constraints AS (
  SELECT jsonb_build_object(
    'input_image_resolution_range',
    '{"min":{"long_edge":300,"short_edge":300},"max":{"long_edge":6000,"short_edge":6000}}'::jsonb,
    'input_image_aspect_ratio_range',
    '[0.4,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.provider_key = 'volces'
    AND (
      base_model.canonical_model_key IN (
        'volces:doubao-seedance-2-0-260128',
        'volces:doubao-seedance-2-0-fast-260128',
        'volces:doubao-seedance-2-0-mini-260615'
      )
      OR base_model.provider_model_name IN (
        'doubao-seedance-2-0-260128',
        'doubao-seedance-2-0-fast-260128',
        'doubao-seedance-2-0-mini-260615'
      )
    )
),
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.provider = 'volces'
  AND (
    platform_model.base_model_id IN (SELECT id FROM updated_base_models)
    OR COALESCE(NULLIF(platform_model.provider_model_name, ''), platform_model.model_name) IN (
      'doubao-seedance-2-0-260128',
      'doubao-seedance-2-0-fast-260128',
      'doubao-seedance-2-0-mini-260615'
    )
  );
