-- v84: Add founding1k to profiles_admin_badge_ids_allowed and apply to all current profiles with public_uid <= 1000
alter table public.profiles
  drop constraint if exists profiles_admin_badge_ids_allowed;

alter table public.profiles
  add constraint profiles_admin_badge_ids_allowed check (
    admin_badge_ids <@ array[
      'owner',
      'staff',
      'helper',
      'verified',
      'premium',
      'donor',
      'og',
      'rich',
      'bug',
      'winner',
      'early',
      'founding100',
      'founding1k',
      'clown',
      'discord-server-booster',
      'halloween-2026',
      'halloween-2026-winner'
    ]::text[]
  );

-- Populate founding1k badge in badges jsonb for all eligible existing accounts
UPDATE public.profiles
SET badges = COALESCE(badges, '[]'::jsonb) || jsonb_build_array(
  jsonb_build_object(
    'id', 'founding1k',
    'name', 'Founding 1,000',
    'icon', '✨',
    'custom', false,
    'enabled', true,
    'monoIcon', '✦',
    'image_url', ''
  )
)
WHERE public_uid > 0 
  AND public_uid <= 1000
  AND (badges IS NULL OR NOT (badges::text LIKE '%"founding1k"%'));
