CREATE TABLE IF NOT EXISTS "inventory_item" (
	"sku_id" uuid PRIMARY KEY,
	"vendor_id" text,
	"quantity_total" integer,
	"quantity_sold" integer DEFAULT 0 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 IF NOT EXISTS "stock_reservation" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
	"sku_id" uuid NOT NULL,
	"qty" integer NOT NULL,
	"holder_ref" text NOT NULL,
	"expires_at" timestamp with time zone NOT NULL,
	"consumed_at" timestamp with time zone,
	"released_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "uq_active_holder" ON "stock_reservation" ("sku_id", "holder_ref") WHERE "consumed_at" IS NULL AND "released_at" IS NULL;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "ix_active_sku" ON "stock_reservation" ("sku_id") WHERE "consumed_at" IS NULL AND "released_at" IS NULL;
