UPDATE creative_projects AS project
SET cast_members = (
    SELECT jsonb_agg(
        CASE
            WHEN jsonb_typeof(member.value->'follower_count') = 'number'
            THEN jsonb_set(
                member.value,
                '{follower_count}',
                to_jsonb(member.value->>'follower_count')
            )
            ELSE member.value
        END
        ORDER BY member.ordinality
    ) AS members
    FROM jsonb_array_elements(project.cast_members) WITH ORDINALITY AS member(value, ordinality)
)
WHERE EXISTS (
    SELECT 1
    FROM jsonb_array_elements(project.cast_members) AS member(value)
    WHERE jsonb_typeof(member.value->'follower_count') = 'number'
);

COMMENT ON COLUMN creative_projects.cast_members IS
    'JSON array of cast members; follower_count is stored as text when present.';
