-- T7 cutover: re-point 13 satellite FKs from purchases → order_line (or order),
-- drop purchases table + payment_status + redemption_status enums.

--> statement-breakpoint

-- ── 1. reviews ───────────────────────────────────────────────────────────────
ALTER TABLE "reviews" DROP CONSTRAINT IF EXISTS "reviews_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "reviews" DROP CONSTRAINT IF EXISTS "reviews_purchase_id_unique";
--> statement-breakpoint
ALTER TABLE "reviews" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "reviews" ADD CONSTRAINT "reviews_order_line_id_unique" UNIQUE ("order_line_id");
--> statement-breakpoint
ALTER TABLE "reviews" ADD CONSTRAINT "reviews_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 2. club_memberships ──────────────────────────────────────────────────────
ALTER TABLE "club_memberships" DROP CONSTRAINT IF EXISTS "club_memberships_joined_via_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "club_memberships" RENAME COLUMN "joined_via_purchase_id" TO "joined_via_order_line_id";
--> statement-breakpoint
ALTER TABLE "club_memberships" ADD CONSTRAINT "club_memberships_joined_via_order_line_id_order_line_id_fk"
  FOREIGN KEY ("joined_via_order_line_id") REFERENCES "order_line"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 3. purchase_messages ─────────────────────────────────────────────────────
ALTER TABLE "purchase_messages" DROP CONSTRAINT IF EXISTS "purchase_messages_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "purchase_messages" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "purchase_messages" ADD CONSTRAINT "purchase_messages_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 4. user_gallery_images ───────────────────────────────────────────────────
ALTER TABLE "user_gallery_images" DROP CONSTRAINT IF EXISTS "user_gallery_images_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "user_gallery_images" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "user_gallery_images" ADD CONSTRAINT "user_gallery_images_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 5. group_reservations ────────────────────────────────────────────────────
ALTER TABLE "group_reservations" DROP CONSTRAINT IF EXISTS "group_reservations_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "group_reservations" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "group_reservations" ADD CONSTRAINT "group_reservations_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 6. transaction_cases ─────────────────────────────────────────────────────
ALTER TABLE "transaction_cases" DROP CONSTRAINT IF EXISTS "transaction_cases_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "transaction_cases" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "transaction_cases" ADD CONSTRAINT "transaction_cases_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 7. returns ───────────────────────────────────────────────────────────────
ALTER TABLE "returns" DROP CONSTRAINT IF EXISTS "returns_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "returns" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "returns" ADD CONSTRAINT "returns_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 8. promo_redemptions ─────────────────────────────────────────────────────
ALTER TABLE "promo_redemptions" DROP CONSTRAINT IF EXISTS "promo_redemptions_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "promo_redemptions" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "promo_redemptions" ADD CONSTRAINT "promo_redemptions_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 9. tax_documents ─────────────────────────────────────────────────────────
-- (no FK constraint on purchase_id — just a column rename)
ALTER TABLE "tax_documents" RENAME COLUMN "purchase_id" TO "order_line_id";

--> statement-breakpoint

-- ── 10. shipment_purchases ───────────────────────────────────────────────────
ALTER TABLE "shipment_purchases" DROP CONSTRAINT IF EXISTS "shipment_purchases_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "shipment_purchases" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "shipment_purchases" ADD CONSTRAINT "shipment_purchases_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE RESTRICT ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 11. invoices ─────────────────────────────────────────────────────────────
ALTER TABLE "invoices" DROP CONSTRAINT IF EXISTS "invoices_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "invoices" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 12. vendor_payout_releases ───────────────────────────────────────────────
ALTER TABLE "vendor_payout_releases" DROP CONSTRAINT IF EXISTS "vendor_payout_releases_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "vendor_payout_releases" RENAME COLUMN "purchase_id" TO "order_line_id";
--> statement-breakpoint
ALTER TABLE "vendor_payout_releases" ADD CONSTRAINT "vendor_payout_releases_order_line_id_order_line_id_fk"
  FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 13. support_tickets (→ order.id, not order_line.id) ──────────────────────
ALTER TABLE "support_tickets" DROP CONSTRAINT IF EXISTS "support_tickets_related_purchase_id_purchases_id_fk";
--> statement-breakpoint
ALTER TABLE "support_tickets" RENAME COLUMN "related_purchase_id" TO "related_order_id";
--> statement-breakpoint
ALTER TABLE "support_tickets" ADD CONSTRAINT "support_tickets_related_order_id_order_id_fk"
  FOREIGN KEY ("related_order_id") REFERENCES "order"("id") ON DELETE SET NULL ON UPDATE NO ACTION;

--> statement-breakpoint

-- ── 14. mock_payment_events (no FK — just rename) ────────────────────────────
ALTER TABLE "mock_payment_events" RENAME COLUMN "purchase_id" TO "order_line_id";

--> statement-breakpoint

-- ── 15. Drop purchases table ─────────────────────────────────────────────────
-- All FKs referencing purchases have been dropped above.
DROP TABLE IF EXISTS "purchases" CASCADE;

--> statement-breakpoint

-- ── 16. Drop purchases-only enums ────────────────────────────────────────────
DROP TYPE IF EXISTS "public"."payment_status";
--> statement-breakpoint
DROP TYPE IF EXISTS "public"."redemption_status";
