-- Schedule: per-date, per-agent shift assignment. Ported from the legacy
-- state.schedule[date][agentId] = shiftCode structure (JavaScript.html).
--
-- shift_code stores the exact legacy string value verbatim: REGULAR,
-- EARLY, LATE, SAT, HALF, HOL_WORK, HOL_WORK_<BASE> (e.g. HOL_WORK_LATE),
-- HOL_OFF, NSD_CREDIT, NSD_NOPAY, LEAVE, LWOP, NSD, OFF, or a dynamic
-- CUSTOM_HH:MM-HH:MM value. Left as unconstrained text (no CHECK enum) —
-- the CUSTOM_ form is open-ended by design in the source app, and the
-- ported parsing logic (shiftDurationHours / nightDiffHoursForShift /
-- parseCustomShift in invoiceLogic.js) works directly off this exact
-- string, so there is zero translation risk keeping it as-is.
--
-- agent_id is free text, matching invoices.agent_id / qpi_qualifications
-- .agent_id — there is still no `agents` table (deferred, same as every
-- table besides invoices/qpi_qualifications in the original 19-table
-- plan).

create table if not exists public.schedule (
  id          uuid primary key default gen_random_uuid(),
  shift_date  date not null,
  agent_id    text not null,
  shift_code  text not null,
  updated_at  timestamptz not null default now(),
  unique (shift_date, agent_id)
);

alter table public.schedule enable row level security;

-- Readable by any authenticated user — matches the legacy app's behavior:
-- the whole schedule lived in one shared blob visible to every signed-in
-- admin/agent, not just the owner.
create policy "schedule_select_all"
on public.schedule
for select
to authenticated
using (true);

-- TODO: intentionally permissive for now, same pattern invoices had in
-- 0001 ("Authenticated users can view invoices") before real RLS existed
-- in 0008. There is no admin/role concept on `profiles` yet — it only has
-- agent_id/is_invoice_reviewer/is_invoice_approver. Once a real is_admin
-- (or equivalent) flag exists, replace `true` below with that check, so
-- only admins can write the schedule — matching the legacy app's
-- canEditSchedule() (=== role admin).
create policy "schedule_insert_admin_TODO"
on public.schedule
for insert
to authenticated
with check (true);

create policy "schedule_update_admin_TODO"
on public.schedule
for update
to authenticated
using (true)
with check (true);
