-- expense-ocr-correction-ux (wave-13): extend expenses + expense_corrections tables.
-- All ADD COLUMN statements use IF NOT EXISTS for idempotent deploys.

-- expense_corrections: discriminate OCR-review vs manual post-approval corrections.
ALTER TABLE "expense_corrections" ADD COLUMN IF NOT EXISTS "correction_source" text NOT NULL DEFAULT 'ocr';
--> statement-breakpoint
ALTER TABLE "expense_corrections" ADD CONSTRAINT "expense_corrections_correction_source_check"
  CHECK (correction_source IN ('ocr', 'manual'));
--> statement-breakpoint

-- expenses: post-approval correction note + void (soft-delete) columns.
ALTER TABLE "expenses" ADD COLUMN IF NOT EXISTS "correction_note" text;
--> statement-breakpoint
ALTER TABLE "expenses" ADD COLUMN IF NOT EXISTS "voided_at" timestamp with time zone;
--> statement-breakpoint
ALTER TABLE "expenses" ADD COLUMN IF NOT EXISTS "voided_reason" text;
--> statement-breakpoint

-- Backs the NEEDS_REVIEW queue and enforces the void-exclusion read path cheaply.
CREATE INDEX IF NOT EXISTS idx_expenses_review_queue
  ON expenses (tenant_id, status)
  WHERE voided_at IS NULL;
