-- 100: Recurring (Stripe Subscription) support for compliance services.
--
-- The OIG/SAM exclusion screening is sold as a $79/month Stripe Subscription
-- (catalog billing_interval="month"). We track the Stripe Subscription id so the
-- invoice.paid renewal webhook can map a monthly charge back to the order and
-- re-run that cycle's screening.
--
-- The Provider Compliance Bundle ($899/yr) includes only the FIRST OIG/SAM
-- screening; bundle buyers are converted to the $79/mo monitoring subscription
-- after that first cycle via an automated upsell email. bundle_upsell_sent_at
-- dedupes that send.
--
-- Idempotent. (Mirrors ensureColumns() in api/src/routes/checkout.ts so a fresh
-- deploy and this migration converge on the same schema.)

ALTER TABLE compliance_orders
  ADD COLUMN IF NOT EXISTS stripe_subscription_id text;

ALTER TABLE compliance_orders
  ADD COLUMN IF NOT EXISTS bundle_upsell_sent_at timestamptz;

-- Map a renewal invoice's subscription back to its order quickly.
CREATE INDEX IF NOT EXISTS idx_compliance_orders_subscription
  ON compliance_orders (stripe_subscription_id)
  WHERE stripe_subscription_id IS NOT NULL;
