CREATE TYPE "public"."account_state_user" AS ENUM('ACTIVE', 'FROZEN', 'DELETED_PENDING', 'DELETED');--> statement-breakpoint
CREATE TYPE "public"."account_state_vendor" AS ENUM('PENDING_FIRST_APPROVAL', 'ACTIVE', 'VETERAN', 'FROZEN', 'BANNED');--> statement-breakpoint
CREATE TYPE "public"."admin_action" AS ENUM('APPROVE', 'REJECT', 'FREEZE', 'UNFREEZE', 'BAN', 'REMOVE_REVIEW', 'RESTORE_REVIEW', 'ADD_QUANTITY');--> statement-breakpoint
CREATE TYPE "public"."admin_target_type" AS ENUM('DEAL', 'VENDOR', 'REVIEW', 'USER');--> statement-breakpoint
CREATE TYPE "public"."approved_by" AS ENUM('HUMAN', 'AI_AGENT');--> statement-breakpoint
CREATE TYPE "public"."avatar_type" AS ENUM('ICON', 'GRAVATAR');--> statement-breakpoint
CREATE TYPE "public"."deal_state" AS ENUM('DRAFT', 'PENDING_APPROVAL', 'ACTIVE', 'PAUSED', 'SOLD_OUT', 'EXPIRED', 'REJECTED');--> statement-breakpoint
CREATE TYPE "public"."deal_type" AS ENUM('DAILY', 'EXPERIENCE', 'PRODUCT', 'VOUCHER');--> statement-breakpoint
CREATE TYPE "public"."image_approval_status" AS ENUM('PENDING', 'APPROVED', 'REJECTED', 'AUTO_APPROVED');--> statement-breakpoint
CREATE TYPE "public"."payment_brand" AS ENUM('VISA', 'MASTERCARD', 'AMEX', 'BIT', 'PAYPAL', 'GOOGLE_PAY', 'OTHER');--> statement-breakpoint
CREATE TYPE "public"."payment_status" AS ENUM('PENDING', 'COMPLETED', 'REFUNDED', 'PARTIAL_REFUND');--> statement-breakpoint
CREATE TYPE "public"."payout_status" AS ENUM('PENDING', 'PROCESSING', 'COMPLETED', 'FAILED');--> statement-breakpoint
CREATE TYPE "public"."personal_deal_status" AS ENUM('PENDING', 'ACCEPTED', 'REJECTED', 'EXPIRED');--> statement-breakpoint
CREATE TYPE "public"."redemption_status" AS ENUM('UNREDEEMED', 'REDEEMED', 'EXPIRED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."removal_status" AS ENUM('NONE', 'PENDING', 'APPROVED', 'REJECTED');--> statement-breakpoint
CREATE TYPE "public"."removed_by" AS ENUM('HUMAN', 'AI_AGENT');--> statement-breakpoint
CREATE TYPE "public"."report_reason" AS ENUM('FACTUAL_ERROR', 'MISLEADING', 'WRONG_HOURS', 'BUG', 'OTHER');--> statement-breakpoint
CREATE TYPE "public"."report_status" AS ENUM('OPEN', 'INVESTIGATING', 'RESOLVED', 'DISMISSED');--> statement-breakpoint
CREATE TYPE "public"."report_target_type" AS ENUM('VENDOR', 'DEAL', 'REVIEW', 'USER');--> statement-breakpoint
CREATE TYPE "public"."review_removal_queue_status" AS ENUM('PENDING', 'APPROVED', 'REJECTED');--> statement-breakpoint
CREATE TYPE "public"."review_type" AS ENUM('STANDARD', 'TECHNICAL');--> statement-breakpoint
CREATE TYPE "public"."scan_status" AS ENUM('PENDING', 'CLEAN', 'INFECTED', 'ERROR');--> statement-breakpoint
CREATE TYPE "public"."sender_type" AS ENUM('USER', 'VENDOR');--> statement-breakpoint
CREATE TYPE "public"."vendor_tier" AS ENUM('NEW', 'VETERAN');--> statement-breakpoint
CREATE TABLE "addresses" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"label" text NOT NULL,
	"full_address" text NOT NULL,
	"lat" numeric(10, 7) NOT NULL,
	"lng" numeric(10, 7) NOT NULL,
	"is_gps" boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE "admin_actions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"admin_id" uuid,
	"target_type" "admin_target_type" NOT NULL,
	"target_id" uuid NOT NULL,
	"action" "admin_action" NOT NULL,
	"note" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "business_hours" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"vendor_id" uuid NOT NULL,
	"monday_open" time,
	"monday_close" time,
	"monday_closed" boolean DEFAULT false NOT NULL,
	"tuesday_open" time,
	"tuesday_close" time,
	"tuesday_closed" boolean DEFAULT false NOT NULL,
	"wednesday_open" time,
	"wednesday_close" time,
	"wednesday_closed" boolean DEFAULT false NOT NULL,
	"thursday_open" time,
	"thursday_close" time,
	"thursday_closed" boolean DEFAULT false NOT NULL,
	"friday_open" time,
	"friday_close" time,
	"friday_closed" boolean DEFAULT false NOT NULL,
	"saturday_open" time,
	"saturday_close" time,
	"saturday_closed" boolean DEFAULT true NOT NULL,
	"sunday_open" time,
	"sunday_close" time,
	"sunday_closed" boolean DEFAULT false NOT NULL,
	"special_notes" text,
	CONSTRAINT "business_hours_vendor_id_unique" UNIQUE("vendor_id")
);
--> statement-breakpoint
CREATE TABLE "deal_images" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"deal_id" uuid NOT NULL,
	"url" text NOT NULL,
	"is_primary" boolean DEFAULT false NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"approval_status" "image_approval_status" DEFAULT 'PENDING' NOT NULL
);
--> statement-breakpoint
CREATE TABLE "deals" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"vendor_id" uuid NOT NULL,
	"deal_type" "deal_type" NOT NULL,
	"title" text NOT NULL,
	"description" text DEFAULT '' NOT NULL,
	"category" text,
	"original_price" numeric(10, 2) NOT NULL,
	"discount_percent" integer NOT NULL,
	"discounted_price" numeric(10, 2) NOT NULL,
	"quantity_total" integer NOT NULL,
	"quantity_sold" integer DEFAULT 0 NOT NULL,
	"window_start" timestamp with time zone,
	"window_end" timestamp with time zone,
	"pickup_start" time,
	"pickup_end" time,
	"pickup_address" text DEFAULT '' NOT NULL,
	"special_instructions" text,
	"deal_state" "deal_state" DEFAULT 'DRAFT' NOT NULL,
	"rejection_reason" text,
	"rejection_detail" text,
	"commission_rate" numeric(4, 3) DEFAULT '0.100' NOT NULL,
	"is_personal_deal" boolean DEFAULT false NOT NULL,
	"personal_deal_for_user_id" uuid,
	"approved_at" timestamp with time zone,
	"approved_by" "approved_by",
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"sold_out_at" timestamp with time zone,
	"sold_out_gold_expires_at" timestamp with time zone,
	CONSTRAINT "deals_discount_percent_range" CHECK ("deals"."discount_percent" BETWEEN 50 AND 100)
);
--> statement-breakpoint
CREATE TABLE "image_uploads" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"uploader_user_id" uuid,
	"uploader_vendor_id" uuid,
	"r2_key" text NOT NULL,
	"mime" text NOT NULL,
	"size_bytes" integer NOT NULL,
	"scan_status" "scan_status" DEFAULT 'PENDING' NOT NULL,
	"approval_status" "image_approval_status" DEFAULT 'PENDING' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "loyalty_memberships" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"vendor_id" uuid NOT NULL,
	"joined_via_purchase_id" uuid NOT NULL,
	"joined_at" timestamp with time zone DEFAULT now() NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	CONSTRAINT "loyalty_user_vendor_unique" UNIQUE("user_id","vendor_id")
);
--> statement-breakpoint
CREATE TABLE "magic_links" (
	"id" text PRIMARY KEY NOT NULL,
	"token_hash" text NOT NULL,
	"email_encrypted" text NOT NULL,
	"pre_fill" jsonb,
	"used_at" timestamp with time zone,
	"expires_at" timestamp with time zone NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "magic_links_token_hash_unique" UNIQUE("token_hash")
);
--> statement-breakpoint
CREATE TABLE "monthly_settlements" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"vendor_id" uuid NOT NULL,
	"period_start" date NOT NULL,
	"period_end" date NOT NULL,
	"total_deals_revenue" numeric(12, 2) DEFAULT '0' NOT NULL,
	"total_loyalty_revenue" numeric(12, 2) DEFAULT '0' NOT NULL,
	"platform_commission" numeric(12, 2) DEFAULT '0' NOT NULL,
	"vendor_payout" numeric(12, 2) DEFAULT '0' NOT NULL,
	"payout_method" text NOT NULL,
	"payout_status" "payout_status" DEFAULT 'PENDING' NOT NULL,
	"payout_at" timestamp with time zone,
	"morning_payout_id" text
);
--> statement-breakpoint
CREATE TABLE "outbox" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"aggregate_type" text NOT NULL,
	"aggregate_id" uuid NOT NULL,
	"event_type" text NOT NULL,
	"payload" jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"processed_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "payment_methods" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"last4" text NOT NULL,
	"brand" "payment_brand" NOT NULL,
	"token" text NOT NULL,
	"is_default" boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE "personal_deal_requests" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"vendor_id" uuid NOT NULL,
	"source_deal_id" uuid NOT NULL,
	"status" "personal_deal_status" DEFAULT 'PENDING' NOT NULL,
	"response_deadline" timestamp with time zone NOT NULL,
	"responded_at" timestamp with time zone,
	"created_deal_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "purchase_messages" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"purchase_id" uuid NOT NULL,
	"sender_type" "sender_type" NOT NULL,
	"sender_id" uuid NOT NULL,
	"body" text NOT NULL,
	"read_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "purchases" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid,
	"guest_email" text,
	"guest_phone" text,
	"deal_id" uuid NOT NULL,
	"vendor_id" uuid NOT NULL,
	"quantity" integer DEFAULT 1 NOT NULL,
	"amount_paid" numeric(10, 2) NOT NULL,
	"commission_amount" numeric(10, 2) NOT NULL,
	"vendor_amount" numeric(10, 2) NOT NULL,
	"payment_method_id" uuid,
	"payment_status" "payment_status" DEFAULT 'PENDING' NOT NULL,
	"redemption_status" "redemption_status" DEFAULT 'UNREDEEMED' NOT NULL,
	"qr_token_hash" text NOT NULL,
	"qr_png_url" text,
	"redeemed_at" timestamp with time zone,
	"expires_at" timestamp with time zone NOT NULL,
	"cancelled_at" timestamp with time zone,
	"cancellation_reason" text,
	"review_eligible" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"morning_charge_id" text,
	"morning_invoice_id" text,
	"idempotency_key" text NOT NULL,
	CONSTRAINT "purchases_idempotency_key_unique" UNIQUE("idempotency_key")
);
--> statement-breakpoint
CREATE TABLE "push_subscriptions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"endpoint" text NOT NULL,
	"p256dh" text NOT NULL,
	"auth" text NOT NULL,
	"user_agent" text DEFAULT '' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"revoked_at" timestamp with time zone,
	CONSTRAINT "push_subscriptions_endpoint_unique" UNIQUE("endpoint")
);
--> statement-breakpoint
CREATE TABLE "rate_limit_buckets" (
	"key" text PRIMARY KEY NOT NULL,
	"count" integer DEFAULT 0 NOT NULL,
	"window_start_at" timestamp with time zone NOT NULL,
	"refilled_at" timestamp with time zone NOT NULL
);
--> statement-breakpoint
CREATE TABLE "report_tickets" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"reporter_user_id" uuid,
	"target_type" "report_target_type" NOT NULL,
	"target_id" uuid NOT NULL,
	"reason" "report_reason" NOT NULL,
	"body" text DEFAULT '' NOT NULL,
	"status" "report_status" DEFAULT 'OPEN' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"resolved_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "review_removal_queue" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"review_id" uuid NOT NULL,
	"submitted_by" uuid NOT NULL,
	"reason" text NOT NULL,
	"ai_score" numeric(5, 4),
	"status" "review_removal_queue_status" DEFAULT 'PENDING' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"decided_at" timestamp with time zone,
	CONSTRAINT "review_removal_queue_review_id_unique" UNIQUE("review_id")
);
--> statement-breakpoint
CREATE TABLE "reviews" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"purchase_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"vendor_id" uuid NOT NULL,
	"review_type" "review_type" DEFAULT 'STANDARD' NOT NULL,
	"rating" integer,
	"body" text NOT NULL,
	"vendor_reply" text,
	"vendor_reply_at" timestamp with time zone,
	"removal_requested" boolean DEFAULT false NOT NULL,
	"removal_reason" text,
	"removal_status" "removal_status" DEFAULT 'NONE' NOT NULL,
	"removed_by" "removed_by",
	"is_visible" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "reviews_purchase_id_unique" UNIQUE("purchase_id"),
	CONSTRAINT "reviews_rating_range" CHECK ("reviews"."rating" IS NULL OR ("reviews"."rating" BETWEEN 1 AND 5))
);
--> statement-breakpoint
CREATE TABLE "sessions" (
	"id" text PRIMARY KEY NOT NULL,
	"user_id" uuid NOT NULL,
	"csrf_token" text NOT NULL,
	"user_agent" text DEFAULT '' NOT NULL,
	"ip_encrypted" text,
	"expires_at" timestamp with time zone NOT NULL,
	"revoked_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "standing_offers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"vendor_id" uuid NOT NULL,
	"title" text NOT NULL,
	"description" text DEFAULT '' NOT NULL,
	"image_url" text,
	"original_price" numeric(10, 2) NOT NULL,
	"discount_percent" integer DEFAULT 0 NOT NULL,
	"discounted_price" numeric(10, 2) NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "standing_offers_discount_range" CHECK ("standing_offers"."discount_percent" BETWEEN 0 AND 100)
);
--> statement-breakpoint
CREATE TABLE "user_gallery_images" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"vendor_id" uuid NOT NULL,
	"purchase_id" uuid,
	"url" text NOT NULL,
	"caption" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "users" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"phone" text NOT NULL,
	"phone_index" text NOT NULL,
	"email" text,
	"email_index" text,
	"display_name" text DEFAULT '' NOT NULL,
	"avatar_type" "avatar_type" DEFAULT 'ICON' NOT NULL,
	"avatar_value" text DEFAULT '' NOT NULL,
	"email_2fa_enabled" boolean DEFAULT true NOT NULL,
	"purchase_count" integer DEFAULT 0 NOT NULL,
	"preferences_profile" jsonb,
	"account_state" "account_state_user" DEFAULT 'ACTIVE' NOT NULL,
	"is_admin" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"deletion_requested_at" timestamp with time zone,
	CONSTRAINT "users_phone_index_unique" UNIQUE("phone_index"),
	CONSTRAINT "users_email_index_unique" UNIQUE("email_index")
);
--> statement-breakpoint
CREATE TABLE "vendor_addresses" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"vendor_id" uuid NOT NULL,
	"label" text NOT NULL,
	"full_address" text NOT NULL,
	"lat" numeric(10, 7) NOT NULL,
	"lng" numeric(10, 7) NOT NULL
);
--> statement-breakpoint
CREATE TABLE "vendor_gallery_images" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"vendor_id" uuid NOT NULL,
	"url" text NOT NULL,
	"caption" text,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "vendors" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"owner_user_id" uuid NOT NULL,
	"business_name" text NOT NULL,
	"display_name" text NOT NULL,
	"description" text DEFAULT '' NOT NULL,
	"category" text[] DEFAULT ARRAY[]::text[] NOT NULL,
	"phone" text NOT NULL,
	"email" text NOT NULL,
	"logo_url" text,
	"hero_image_url" text,
	"tier" "vendor_tier" DEFAULT 'NEW' NOT NULL,
	"account_state" "account_state_vendor" DEFAULT 'PENDING_FIRST_APPROVAL' NOT NULL,
	"total_sales" integer DEFAULT 0 NOT NULL,
	"total_revenue" numeric(12, 2) DEFAULT '0' NOT NULL,
	"reviews_score" numeric(3, 2) DEFAULT '0' NOT NULL,
	"reviews_count" integer DEFAULT 0 NOT NULL,
	"removed_reviews_count" integer DEFAULT 0 NOT NULL,
	"unresolved_voucher_complaints" integer DEFAULT 0 NOT NULL,
	"first_voucher_complaint_at" timestamp with time zone,
	"payout_method" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "addresses" ADD CONSTRAINT "addresses_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "admin_actions" ADD CONSTRAINT "admin_actions_admin_id_users_id_fk" FOREIGN KEY ("admin_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "business_hours" ADD CONSTRAINT "business_hours_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "deal_images" ADD CONSTRAINT "deal_images_deal_id_deals_id_fk" FOREIGN KEY ("deal_id") REFERENCES "public"."deals"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "deals" ADD CONSTRAINT "deals_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "deals" ADD CONSTRAINT "deals_personal_deal_for_user_id_users_id_fk" FOREIGN KEY ("personal_deal_for_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "image_uploads" ADD CONSTRAINT "image_uploads_uploader_user_id_users_id_fk" FOREIGN KEY ("uploader_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "image_uploads" ADD CONSTRAINT "image_uploads_uploader_vendor_id_vendors_id_fk" FOREIGN KEY ("uploader_vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loyalty_memberships" ADD CONSTRAINT "loyalty_memberships_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loyalty_memberships" ADD CONSTRAINT "loyalty_memberships_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loyalty_memberships" ADD CONSTRAINT "loyalty_memberships_joined_via_purchase_id_purchases_id_fk" FOREIGN KEY ("joined_via_purchase_id") REFERENCES "public"."purchases"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "monthly_settlements" ADD CONSTRAINT "monthly_settlements_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "payment_methods" ADD CONSTRAINT "payment_methods_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "personal_deal_requests" ADD CONSTRAINT "personal_deal_requests_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "personal_deal_requests" ADD CONSTRAINT "personal_deal_requests_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "personal_deal_requests" ADD CONSTRAINT "personal_deal_requests_source_deal_id_deals_id_fk" FOREIGN KEY ("source_deal_id") REFERENCES "public"."deals"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "personal_deal_requests" ADD CONSTRAINT "personal_deal_requests_created_deal_id_deals_id_fk" FOREIGN KEY ("created_deal_id") REFERENCES "public"."deals"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "purchase_messages" ADD CONSTRAINT "purchase_messages_purchase_id_purchases_id_fk" FOREIGN KEY ("purchase_id") REFERENCES "public"."purchases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_deal_id_deals_id_fk" FOREIGN KEY ("deal_id") REFERENCES "public"."deals"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_payment_method_id_payment_methods_id_fk" FOREIGN KEY ("payment_method_id") REFERENCES "public"."payment_methods"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "push_subscriptions" ADD CONSTRAINT "push_subscriptions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "report_tickets" ADD CONSTRAINT "report_tickets_reporter_user_id_users_id_fk" FOREIGN KEY ("reporter_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "review_removal_queue" ADD CONSTRAINT "review_removal_queue_review_id_reviews_id_fk" FOREIGN KEY ("review_id") REFERENCES "public"."reviews"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "review_removal_queue" ADD CONSTRAINT "review_removal_queue_submitted_by_vendors_id_fk" FOREIGN KEY ("submitted_by") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "reviews" ADD CONSTRAINT "reviews_purchase_id_purchases_id_fk" FOREIGN KEY ("purchase_id") REFERENCES "public"."purchases"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "reviews" ADD CONSTRAINT "reviews_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "reviews" ADD CONSTRAINT "reviews_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "standing_offers" ADD CONSTRAINT "standing_offers_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_gallery_images" ADD CONSTRAINT "user_gallery_images_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_gallery_images" ADD CONSTRAINT "user_gallery_images_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_gallery_images" ADD CONSTRAINT "user_gallery_images_purchase_id_purchases_id_fk" FOREIGN KEY ("purchase_id") REFERENCES "public"."purchases"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vendor_addresses" ADD CONSTRAINT "vendor_addresses_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vendor_gallery_images" ADD CONSTRAINT "vendor_gallery_images_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vendors" ADD CONSTRAINT "vendors_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "deals_vendor_state_idx" ON "deals" USING btree ("vendor_id","deal_state");--> statement-breakpoint
CREATE INDEX "deals_type_state_idx" ON "deals" USING btree ("deal_type","deal_state");--> statement-breakpoint
CREATE INDEX "loyalty_vendor_idx" ON "loyalty_memberships" USING btree ("vendor_id");--> statement-breakpoint
CREATE INDEX "magic_links_expires_at_idx" ON "magic_links" USING btree ("expires_at");--> statement-breakpoint
CREATE INDEX "outbox_processed_at_idx" ON "outbox" USING btree ("processed_at");--> statement-breakpoint
CREATE INDEX "purchases_user_created_idx" ON "purchases" USING btree ("user_id","created_at");--> statement-breakpoint
CREATE INDEX "purchases_vendor_idx" ON "purchases" USING btree ("vendor_id");--> statement-breakpoint
CREATE INDEX "reviews_vendor_visible_idx" ON "reviews" USING btree ("vendor_id","is_visible");--> statement-breakpoint
CREATE INDEX "sessions_expires_at_idx" ON "sessions" USING btree ("expires_at");--> statement-breakpoint
CREATE INDEX "users_phone_index_idx" ON "users" USING btree ("phone_index");--> statement-breakpoint
CREATE INDEX "users_email_index_idx" ON "users" USING btree ("email_index");