-- Two related, small changes:
--
-- 1. profiles.is_agent: distinguishes the 5 real CS agents (Mayvel, Mon,
--    Jurina, Kate, Rubyrose) from the 7 management/reviewer accounts
--    (Edwin, Quinty, Dominic, Andrew, Kenn, Bjorn, Berry), matching
--    legacy's own DEFAULT_AGENTS list, which never included any
--    management/reviewer names. Defaults false so nobody is silently
--    marked an agent by omission; the 5 real agents are explicitly
--    flipped true below by their confirmed agent_id slugs (0022).
--
--    This is a DIFFERENT axis from is_active (0029) — is_active is
--    "on/off the roster right now" (any team member, agent or not);
--    is_agent is "is this person one of the 5 CS agents at all," which
--    never changes day to day. /api/agent-roster (feeding /schedule)
--    should only ever show real agents, so it now filters on both.
--
-- 2. invoice_profiles.full_name: seeds Edwin's row with "Edwin" (first
--    name only, no last name confirmed yet). Everyone else's full_name
--    is left '' (the column's existing default, 0012), which the UI
--    already renders as a fallback to agent_id/"—" — so this is purely
--    additive, nothing else changes behavior for the other 11 until
--    their real names are confirmed and added the same way.

alter table public.profiles
  add column if not exists is_agent boolean not null default false;

update public.profiles
set is_agent = true
where agent_id in ('mayvel', 'mon', 'jurina', 'kate', 'rubyrose');

insert into public.invoice_profiles (agent_id, full_name)
values ('agent_042', 'Edwin')
on conflict (agent_id) do update set full_name = excluded.full_name;

do $migration_guard$
declare
  col_exists boolean;
  agent_true_count integer;
  edwin_name text;
begin
  select exists (
    select 1 from information_schema.columns
    where table_schema = 'public' and table_name = 'profiles' and column_name = 'is_agent'
  ) into col_exists;
  if not col_exists then
    raise exception 'Aborting 0031: profiles.is_agent column was not created';
  end if;

  select count(*) into agent_true_count
  from public.profiles
  where is_agent = true;
  if agent_true_count != 5 then
    raise exception 'Aborting 0031: expected exactly 5 profiles with is_agent=true, found %', agent_true_count;
  end if;

  select full_name into edwin_name
  from public.invoice_profiles
  where agent_id = 'agent_042';
  if edwin_name is distinct from 'Edwin' then
    raise exception 'Aborting 0031: invoice_profiles.full_name for agent_042 is %, expected Edwin', edwin_name;
  end if;
end $migration_guard$;
