-- 087: Daily intake-reminder tracking on compliance_orders.
--
-- After a customer pays, we email them an intake form link for each service so
-- we can collect the data needed to prepare the filing. Some customers never
-- complete intake, which stalls fulfillment. The intake-reminder worker
-- (scripts/workers/intake_reminder.py) runs daily at noon ET and nudges any
-- PAID order whose intake is still incomplete (intake_data_validated IS NOT
-- TRUE), up to a cap of 10 reminders, skipping placeholder/invalid emails.
--
-- These two columns make the daily run idempotent and bounded:
--   intake_reminder_count    -- how many reminders we've sent (cap: 10)
--   intake_reminder_last_at  -- when we last reminded (so we send at most 1/day)
--
-- Both default to a no-reminders-yet state and are NULL/0 for every existing
-- row, so the worker treats all currently-incomplete paid orders as eligible.

ALTER TABLE compliance_orders
  ADD COLUMN IF NOT EXISTS intake_reminder_count integer NOT NULL DEFAULT 0;

ALTER TABLE compliance_orders
  ADD COLUMN IF NOT EXISTS intake_reminder_last_at timestamptz;

-- Speeds up the daily eligibility scan (paid + incomplete intake).
CREATE INDEX IF NOT EXISTS idx_compliance_orders_intake_reminder
  ON compliance_orders (payment_status, intake_data_validated, intake_reminder_count)
  WHERE payment_status = 'paid';
