-- Flags an invoice as agent-created directly (blank line-item editor,
-- no schedule-derived weeks/hours), vs. the existing schedule-driven
-- generation flow. Everything else a manual invoice needs is already
-- schema-compatible with no change: weeks defaults to '[]'::jsonb
-- (0001), monthly_base/rate_snapshot/profile_snapshot/bill_to are all
-- nullable, and invoices_insert_own (0008) only requires
-- agent_id = current_agent_id() and status = 'draft' -- nothing
-- schedule/id-shape specific, so no RLS change is needed either.
--
-- Two decisions confirmed rather than assumed:
--   1. Manual invoices get their own id scheme (inv_manual_<uuid>,
--      generated client-side in the manual-creation UI, Stage 3) --
--      fully decoupled from generateInvoiceId()'s deterministic
--      inv_<agentId>_<month>_c<cut> format, so a manual invoice can
--      never collide with (or be silently overwritten by) a
--      schedule-derived invoice for the same agent+month+cut.
--   2. cut_number stays required (not null, check in (1,2)) -- a
--      manual invoice just picks 1 or 2 as a period label. No schema
--      relaxation needed: QPI's billable-base calc and everything
--      else that reads cut_number-adjacent data keys off month +
--      status only, never cut_number itself.

alter table public.invoices
  add column if not exists is_manual boolean not null default false;

do $migration_guard$
declare
  col_exists boolean;
begin
  select exists (
    select 1 from information_schema.columns
    where table_schema = 'public' and table_name = 'invoices' and column_name = 'is_manual'
  ) into col_exists;
  if not col_exists then
    raise exception 'Aborting 0033: invoices.is_manual column was not created';
  end if;
end $migration_guard$;
