-- Grants Andrew and Dominic feature_key='performance' (can_view=true,
-- can_write=true) -- QA/Trustpilot/Penalties/Admin Bonus editing access
-- on /performance and /settings/performance.
--
-- UUIDs confirmed against this project's own prior migrations before
-- writing this, not assumed from memory:
--   - Andrew  = fd91642c-71ec-4716-b275-efc08564a625 (seeded in 0017
--     with comment "-- Andrew <andrew@khaizenunderwear.com>"; also used
--     as "Andrew" 4x in 0020).
--   - Dominic = c04f7a4a-ca51-4d6e-b146-f35e51b57e01 (seeded in 0017
--     with comment "-- Dominic <dominic@khaizenunderwear.com>"; also
--     used as "Dominic" 4x in 0020).
-- (This cross-check caught a real bug in the *other* pending migration,
-- 0038, which had these two swapped for its 'coaching' grant -- fixed
-- there separately, unrelated to this file.)
--
-- Edwin needs no row here -- is_admin already bypasses has_permission()
-- entirely (see has_permission()'s own is_admin clause, 0018/0038).
--
-- Nothing else about either profile changes: no is_admin, no other
-- feature_key touched, feature_registry/has_permission() untouched
-- (feature_key='performance' already exists since 0021).

insert into public.permissions (user_id, feature_key, can_view, can_write) values
  ('fd91642c-71ec-4716-b275-efc08564a625', 'performance', true, true), -- Andrew
  ('c04f7a4a-ca51-4d6e-b146-f35e51b57e01', 'performance', true, true)  -- Dominic
on conflict (user_id, feature_key) do update
  set can_view = true, can_write = true;

-- ============================================================
-- Self-verification guard
-- ============================================================

do $migration_guard$
declare
  andrew_perm_count int;
  dominic_perm_count int;
begin
  select count(*) into andrew_perm_count from public.permissions
    where user_id = 'fd91642c-71ec-4716-b275-efc08564a625'
      and feature_key = 'performance' and can_view = true and can_write = true;
  if andrew_perm_count <> 1 then
    raise exception 'Aborting 0039: expected Andrew to have exactly 1 performance permissions row with view+write, found %', andrew_perm_count;
  end if;

  select count(*) into dominic_perm_count from public.permissions
    where user_id = 'c04f7a4a-ca51-4d6e-b146-f35e51b57e01'
      and feature_key = 'performance' and can_view = true and can_write = true;
  if dominic_perm_count <> 1 then
    raise exception 'Aborting 0039: expected Dominic to have exactly 1 performance permissions row with view+write, found %', dominic_perm_count;
  end if;
end $migration_guard$;
