-- fixup_public_sync.sql

-- 1. Fix Bindings Insert
INSERT INTO public.identity_bindings (identity_id, uuid, binding_type, binding_value)
SELECT identity_id, '384b0ff44aaaa1f1', identity_type, identity_value
FROM dev.identity_bindings
WHERE identity_value IN ('Person_17', 'Person_4')
ON CONFLICT DO NOTHING;

-- 2. Recalculate Appearance Counts in Public
UPDATE public.person_identities
SET appearance_count = (
    SELECT count(*) 
    FROM public.person_appearances 
    WHERE person_appearances.person_id = person_identities.person_id 
    AND person_appearances.video_uuid = person_identities.video_uuid
)
WHERE video_uuid = '384b0ff44aaaa1f1';

-- 3. Verify Result
SELECT person_id, name, appearance_count FROM public.person_identities 
WHERE video_uuid = '384b0ff44aaaa1f1' AND person_id IN ('Person_4', 'Person_17');
