alter table public.crm_webhook_queue
  add column if not exists provider text,
  add column if not exists last_attempt_at timestamptz,
  add column if not exists last_response_summary text;

do $$
begin
  if not exists (select 1 from pg_constraint where conname = 'crm_webhook_queue_status_check') then
    alter table public.crm_webhook_queue
      add constraint crm_webhook_queue_status_check
      check (status in ('pending', 'processing', 'retrying', 'sent', 'failed', 'discarded'));
  end if;
end $$;

create index if not exists idx_crm_queue_due
  on public.crm_webhook_queue(tenant_id, status, scheduled_at, next_attempt_at);
