CREATE TABLE "order" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"buyer_user_id" uuid,
	"buyer_guest_email" text,
	"status" text DEFAULT 'pending' NOT NULL,
	"currency" text NOT NULL,
	"price_mode" text NOT NULL,
	"subtotal" bigint NOT NULL,
	"tax" bigint NOT NULL,
	"discount" bigint NOT NULL,
	"total" bigint NOT NULL,
	"charge_ref" text,
	"failure_reason" text,
	"idempotency_key" text NOT NULL,
	"request_hash" text NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "order_buyer_chk" CHECK (num_nonnulls("order"."buyer_user_id", "order"."buyer_guest_email") = 1)
);
--> statement-breakpoint
CREATE TABLE "order_guest_contact" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"order_id" uuid NOT NULL,
	"email_enc" text NOT NULL,
	"phone_enc" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "order_line" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"order_id" uuid NOT NULL,
	"variant_id" uuid NOT NULL,
	"kind" text NOT NULL,
	"qty" integer NOT NULL,
	"unit_price" bigint NOT NULL,
	"line_total" bigint NOT NULL,
	"vendor_id" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "order_line_redemption" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"order_line_id" uuid NOT NULL,
	"qr_token_hash" text,
	"qr_png_url" text,
	"status" text DEFAULT 'UNREDEEMED' NOT NULL,
	"redeemed_at" timestamp with time zone,
	"expires_at" timestamp with time zone,
	"review_eligible" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "order_step" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"order_id" uuid NOT NULL,
	"step_id" text NOT NULL,
	"result" jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "refund_intent" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"order_id" uuid NOT NULL,
	"seq" integer NOT NULL,
	"amount" bigint NOT NULL,
	"status" text DEFAULT 'pending' NOT NULL,
	"refund_key" text NOT NULL,
	"provider_ref" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "refund_intent_amount_chk" CHECK ("refund_intent"."amount" > 0)
);
--> statement-breakpoint
CREATE TABLE "vendor_split" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"order_id" uuid NOT NULL,
	"vendor_id" text,
	"amount" bigint NOT NULL,
	"funder" text NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX "uq_order_idempotency_key" ON "order" USING btree ("idempotency_key");--> statement-breakpoint
CREATE UNIQUE INDEX "uq_order_step" ON "order_step" USING btree ("order_id","step_id");--> statement-breakpoint
CREATE UNIQUE INDEX "uq_refund_intent_seq" ON "refund_intent" USING btree ("order_id","seq");--> statement-breakpoint
CREATE INDEX "ix_order_line_order" ON "order_line" USING btree ("order_id");--> statement-breakpoint
CREATE INDEX "ix_refund_intent_order" ON "refund_intent" USING btree ("order_id") WHERE status IN ('pending', 'executed');--> statement-breakpoint
ALTER TABLE "order_guest_contact" ADD CONSTRAINT "order_guest_contact_order_id_fk" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE CASCADE;--> statement-breakpoint
ALTER TABLE "order_line_redemption" ADD CONSTRAINT "order_line_redemption_order_line_id_fk" FOREIGN KEY ("order_line_id") REFERENCES "order_line"("id") ON DELETE CASCADE;