-- wave-14: lead-lost-re-engagement schema delta

-- Add re-engagement scheduling columns to leads
ALTER TABLE leads ADD COLUMN IF NOT EXISTS reengagement_at TIMESTAMPTZ;
--> statement-breakpoint
ALTER TABLE leads ADD COLUMN IF NOT EXISTS reengagement_notified_at TIMESTAMPTZ;
--> statement-breakpoint
-- Add lost reasons config to tenant_settings (JSONB array of label strings)
ALTER TABLE tenant_settings ADD COLUMN IF NOT EXISTS lead_lost_reasons JSONB
  DEFAULT '["Price too high","Chose competitor","Not the right time","No budget","Unresponsive"]'::jsonb;
--> statement-breakpoint
-- Partial index for cron: find LOST leads due for re-engagement that haven't been notified yet
CREATE INDEX IF NOT EXISTS idx_leads_reengagement_due
  ON leads (tenant_id, reengagement_at)
  WHERE stage = 'LOST' AND reengagement_notified_at IS NULL;
