CREATE TYPE "public"."active_status" AS ENUM('ACTIVE', 'INACTIVE');--> statement-breakpoint
CREATE TYPE "public"."actor_type" AS ENUM('USER', 'API_KEY', 'SYSTEM', 'PLATFORM_ADMIN');--> statement-breakpoint
CREATE TYPE "public"."ai_proposal_status" AS ENUM('PROPOSED', 'ACCEPTED', 'REJECTED', 'APPLIED', 'EXPIRED');--> statement-breakpoint
CREATE TYPE "public"."ai_proposal_type" AS ENUM('ASN_FROM_DOCUMENT', 'ORDER_FROM_DOCUMENT', 'RECEIVING_ANOMALY', 'INVENTORY_ANOMALY', 'BILLING_ANOMALY', 'SLOTTING_RECOMMENDATION', 'WORKLOAD_FORECAST', 'DISCREPANCY_EMAIL', 'NL_QUERY');--> statement-breakpoint
CREATE TYPE "public"."allocation_status" AS ENUM('ALLOCATED', 'PICKED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."approval_status" AS ENUM('DRAFT', 'PENDING_APPROVAL', 'APPROVED', 'REJECTED', 'POSTED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."asn_status" AS ENUM('DRAFT', 'EXPECTED', 'SCHEDULED', 'ARRIVED', 'RECEIVING', 'RECEIVED', 'CLOSED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."asn_type" AS ENUM('PURCHASE', 'RETURN', 'TRANSFER_IN', 'CROSS_DOCK');--> statement-breakpoint
CREATE TYPE "public"."billing_cycle" AS ENUM('WEEKLY', 'MONTHLY');--> statement-breakpoint
CREATE TYPE "public"."charge_status" AS ENUM('PENDING', 'INVOICED', 'WAIVED', 'DISPUTED');--> statement-breakpoint
CREATE TYPE "public"."charge_trigger" AS ENUM('ASN_CLOSED', 'ORDER_SHIPPED', 'STORAGE_PERIOD', 'TASK_COMPLETED', 'MANUAL');--> statement-breakpoint
CREATE TYPE "public"."charge_unit" AS ENUM('PER_CBM', 'PER_PALLET', 'PER_CASE', 'PER_PIECE', 'PER_KG', 'PER_ORDER', 'PER_LINE', 'PER_SHIPMENT', 'PER_CONTAINER_20', 'PER_CONTAINER_40', 'PER_TRUCK', 'PER_HOUR', 'PER_DOCUMENT', 'PER_CBM_WEEK', 'PER_CBM_DAY', 'PER_PALLET_WEEK', 'PER_PALLET_DAY', 'FLAT_MONTHLY', 'FLAT');--> statement-breakpoint
CREATE TYPE "public"."connector_type" AS ENUM('REST_WEBHOOK', 'SAP_IDOC', 'SFTP_CSV', 'EMAIL_INTAKE');--> statement-breakpoint
CREATE TYPE "public"."contract_status" AS ENUM('DRAFT', 'ACTIVE', 'EXPIRED', 'TERMINATED');--> statement-breakpoint
CREATE TYPE "public"."count_line_status" AS ENUM('PENDING', 'COUNTED', 'RECOUNT', 'APPROVED', 'VARIANCE_POSTED', 'SKIPPED');--> statement-breakpoint
CREATE TYPE "public"."count_status" AS ENUM('PLANNED', 'RELEASED', 'IN_PROGRESS', 'REVIEW', 'COMPLETED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."count_type" AS ENUM('CYCLE', 'FULL', 'SPOT', 'ABC');--> statement-breakpoint
CREATE TYPE "public"."discrepancy_status" AS ENUM('OPEN', 'REPORTED', 'ACKNOWLEDGED', 'RESOLVED');--> statement-breakpoint
CREATE TYPE "public"."discrepancy_type" AS ENUM('OVER', 'SHORT', 'DAMAGED', 'UNEXPECTED_SKU', 'LOT_MISMATCH', 'EXPIRY_MISMATCH', 'SHELF_LIFE_BELOW_MIN');--> statement-breakpoint
CREATE TYPE "public"."doc_source" AS ENUM('MANUAL', 'API', 'IMPORT', 'AI', 'SYSTEM');--> statement-breakpoint
CREATE TYPE "public"."dock_appointment_status" AS ENUM('BOOKED', 'CONFIRMED', 'ARRIVED', 'IN_PROGRESS', 'COMPLETED', 'NO_SHOW', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."hold_scope" AS ENUM('BALANCE', 'LPN', 'LOT', 'SKU', 'LOCATION');--> statement-breakpoint
CREATE TYPE "public"."inventory_status" AS ENUM('AVAILABLE', 'QUARANTINE', 'DAMAGED', 'HOLD', 'EXPIRED', 'QC_PENDING', 'IN_TRANSIT', 'RESERVED');--> statement-breakpoint
CREATE TYPE "public"."invoice_status" AS ENUM('DRAFT', 'ISSUED', 'SENT', 'PAID', 'PARTIALLY_PAID', 'CANCELLED', 'DISPUTED');--> statement-breakpoint
CREATE TYPE "public"."item_condition" AS ENUM('GOOD', 'DAMAGED', 'REJECTED');--> statement-breakpoint
CREATE TYPE "public"."line_status" AS ENUM('OPEN', 'PARTIAL', 'COMPLETE', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."load_status" AS ENUM('PLANNED', 'LOADING', 'LOADED', 'DISPATCHED', 'DELIVERED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."location_status" AS ENUM('ACTIVE', 'BLOCKED', 'COUNTING', 'INACTIVE');--> statement-breakpoint
CREATE TYPE "public"."location_type" AS ENUM('PALLET_RACK', 'SHELF', 'FLOOR', 'BULK', 'BIN', 'DOCK', 'STAGE', 'PACK_STATION', 'VIRTUAL');--> statement-breakpoint
CREATE TYPE "public"."location_usage" AS ENUM('RESERVE', 'PICK', 'DOCK', 'STAGING', 'QC', 'QUARANTINE', 'DAMAGED', 'RETURNS', 'PACKING', 'CROSS_DOCK');--> statement-breakpoint
CREATE TYPE "public"."lpn_status" AS ENUM('ACTIVE', 'EMPTY', 'SHIPPED', 'CONSUMED');--> statement-breakpoint
CREATE TYPE "public"."lpn_type" AS ENUM('PALLET', 'CASE', 'TOTE', 'CAGE', 'CONTAINER');--> statement-breakpoint
CREATE TYPE "public"."message_direction" AS ENUM('INBOUND', 'OUTBOUND');--> statement-breakpoint
CREATE TYPE "public"."message_status" AS ENUM('RECEIVED', 'PROCESSING', 'PROCESSED', 'FAILED', 'DEAD');--> statement-breakpoint
CREATE TYPE "public"."movement_type" AS ENUM('RECEIPT', 'RECEIPT_REVERSAL', 'PUTAWAY', 'MOVE', 'REPLENISH', 'PICK', 'UNPICK', 'PACK', 'SHIP', 'ADJUST_IN', 'ADJUST_OUT', 'STATUS_CHANGE', 'COUNT_ADJUST', 'RETURN_RECEIPT', 'TRANSFER_OUT', 'TRANSFER_IN', 'CONSUME');--> statement-breakpoint
CREATE TYPE "public"."order_status" AS ENUM('DRAFT', 'OPEN', 'ON_HOLD', 'PARTIALLY_ALLOCATED', 'ALLOCATED', 'RELEASED', 'PICKING', 'PICKED', 'PACKED', 'STAGED', 'LOADED', 'SHIPPED', 'CANCELLED', 'SHORT_CLOSED');--> statement-breakpoint
CREATE TYPE "public"."order_type" AS ENUM('SALES', 'TRANSFER_OUT', 'RETURN_TO_VENDOR', 'SAMPLE', 'DISPOSAL');--> statement-breakpoint
CREATE TYPE "public"."pack_level" AS ENUM('EACH', 'INNER', 'CASE', 'PALLET');--> statement-breakpoint
CREATE TYPE "public"."putaway_strategy" AS ENUM('FIXED_LOCATION', 'SAME_SKU', 'EMPTY_LOCATION', 'ZONE_SEQUENCE', 'NEAREST_TO_DOCK');--> statement-breakpoint
CREATE TYPE "public"."reason_code_type" AS ENUM('ADJUSTMENT', 'DAMAGE', 'HOLD', 'RETURN', 'CANCELLATION', 'COUNT_VARIANCE', 'SHORT_PICK', 'REJECTION', 'VAS');--> statement-breakpoint
CREATE TYPE "public"."return_disposition" AS ENUM('RESTOCK', 'QUARANTINE', 'SCRAP', 'RETURN_TO_VENDOR', 'REPAIR');--> statement-breakpoint
CREATE TYPE "public"."return_status" AS ENUM('REQUESTED', 'AUTHORISED', 'RECEIVING', 'RECEIVED', 'DISPOSITIONED', 'CLOSED', 'REJECTED');--> statement-breakpoint
CREATE TYPE "public"."rotation_rule" AS ENUM('FIFO', 'FEFO', 'LIFO', 'FMFO');--> statement-breakpoint
CREATE TYPE "public"."serial_status" AS ENUM('IN_STOCK', 'ALLOCATED', 'PICKED', 'SHIPPED', 'RETURNED', 'SCRAPPED', 'MISSING');--> statement-breakpoint
CREATE TYPE "public"."shipment_status" AS ENUM('PACKING', 'PACKED', 'STAGED', 'LOADED', 'SHIPPED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."storage_condition" AS ENUM('AMBIENT', 'AIR_CONDITIONED', 'CHILLED', 'FROZEN', 'DEEP_FROZEN', 'HAZMAT');--> statement-breakpoint
CREATE TYPE "public"."storage_method" AS ENUM('WEEKLY_CBM_OPENING_PLUS_IN', 'WEEKLY_CBM_PEAK', 'DAILY_CBM', 'DAILY_PALLET', 'WEEKLY_PALLET_PEAK', 'MONTHLY_FLAT');--> statement-breakpoint
CREATE TYPE "public"."subscription_status" AS ENUM('TRIAL', 'ACTIVE', 'PAST_DUE', 'CANCELLED', 'EXPIRED');--> statement-breakpoint
CREATE TYPE "public"."tariff_group" AS ENUM('HANDLING_IN', 'HANDLING_OUT', 'STORAGE', 'VAS', 'FREIGHT', 'ADMIN', 'OTHER');--> statement-breakpoint
CREATE TYPE "public"."task_status" AS ENUM('PENDING', 'ASSIGNED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'SKIPPED', 'EXCEPTION');--> statement-breakpoint
CREATE TYPE "public"."task_type" AS ENUM('RECEIVE', 'PUTAWAY', 'PICK', 'REPLENISH', 'MOVE', 'COUNT', 'PACK', 'LOAD', 'QC');--> statement-breakpoint
CREATE TYPE "public"."temperature_class" AS ENUM('AMBIENT', 'AIR_CONDITIONED', 'CHILLED', 'FROZEN', 'DEEP_FROZEN', 'HAZMAT');--> statement-breakpoint
CREATE TYPE "public"."tenant_status" AS ENUM('TRIAL', 'ACTIVE', 'SUSPENDED', 'CLOSED');--> statement-breakpoint
CREATE TYPE "public"."user_status" AS ENUM('ACTIVE', 'INVITED', 'DISABLED');--> statement-breakpoint
CREATE TYPE "public"."wave_status" AS ENUM('PLANNED', 'ALLOCATING', 'ALLOCATED', 'RELEASED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."wave_strategy" AS ENUM('BY_ORDER', 'BY_ZONE', 'BATCH');--> statement-breakpoint
CREATE TYPE "public"."zone_type" AS ENUM('STORAGE', 'PICK', 'RECEIVING_DOCK', 'SHIPPING_DOCK', 'STAGING', 'QC', 'QUARANTINE', 'DAMAGED', 'RETURNS', 'PACKING', 'CROSS_DOCK', 'YARD');--> statement-breakpoint
CREATE TABLE "feature_flags" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"key" text NOT NULL,
	"enabled" boolean DEFAULT false NOT NULL,
	"config" jsonb DEFAULT '{}'::jsonb NOT NULL
);
--> statement-breakpoint
CREATE TABLE "plans" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"description" text,
	"monthly_price" numeric(18, 4) DEFAULT 0 NOT NULL,
	"currency" text DEFAULT 'USD' NOT NULL,
	"limits" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"features" text[] DEFAULT '{}' NOT NULL,
	"active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "plans_code_unique" UNIQUE("code")
);
--> statement-breakpoint
CREATE TABLE "platform_users" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"email" text NOT NULL,
	"password_hash" text NOT NULL,
	"name" text NOT NULL,
	"role" text DEFAULT 'SUPPORT' NOT NULL,
	"status" text DEFAULT 'ACTIVE' NOT NULL,
	"last_login_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "platform_users_email_unique" UNIQUE("email")
);
--> statement-breakpoint
CREATE TABLE "subscriptions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"plan_id" uuid NOT NULL,
	"status" "subscription_status" DEFAULT 'TRIAL' NOT NULL,
	"starts_at" timestamp with time zone NOT NULL,
	"ends_at" timestamp with time zone,
	"trial_ends_at" timestamp with time zone,
	"seats" integer DEFAULT 5 NOT NULL,
	"notes" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "tenant_settings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"key" text NOT NULL,
	"value" jsonb NOT NULL
);
--> statement-breakpoint
CREATE TABLE "tenants" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"status" "tenant_status" DEFAULT 'TRIAL' NOT NULL,
	"timezone" text DEFAULT 'Asia/Kuwait' NOT NULL,
	"currency" text DEFAULT 'KWD' NOT NULL,
	"locale" text DEFAULT 'en' NOT NULL,
	"settings" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"branding" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "tenants_code_unique" UNIQUE("code")
);
--> statement-breakpoint
CREATE TABLE "usage_records" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"metric" text NOT NULL,
	"usage_date" date NOT NULL,
	"quantity" numeric(18, 4) 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 "api_keys" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid,
	"name" text NOT NULL,
	"prefix" text NOT NULL,
	"key_hash" text NOT NULL,
	"scopes" text[] DEFAULT '{}' NOT NULL,
	"rate_limit_per_minute" integer DEFAULT 600 NOT NULL,
	"last_used_at" timestamp with time zone,
	"expires_at" timestamp with time zone,
	"revoked_at" timestamp with time zone,
	"created_by_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "api_keys_key_hash_unique" UNIQUE("key_hash")
);
--> statement-breakpoint
CREATE TABLE "roles" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"description" text,
	"permissions" text[] DEFAULT '{}' NOT NULL,
	"is_system" 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 "sessions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"refresh_token_hash" text NOT NULL,
	"user_agent" text,
	"ip" text,
	"device_id" 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,
	CONSTRAINT "sessions_refresh_token_hash_unique" UNIQUE("refresh_token_hash")
);
--> statement-breakpoint
CREATE TABLE "user_roles" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"role_id" uuid NOT NULL,
	"warehouse_id" uuid,
	CONSTRAINT "user_roles_uq" UNIQUE NULLS NOT DISTINCT("user_id","role_id","warehouse_id")
);
--> statement-breakpoint
CREATE TABLE "user_warehouses" (
	"user_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"is_default" boolean DEFAULT false NOT NULL,
	CONSTRAINT "user_warehouses_user_id_warehouse_id_pk" PRIMARY KEY("user_id","warehouse_id")
);
--> statement-breakpoint
CREATE TABLE "users" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"email" text NOT NULL,
	"password_hash" text,
	"name" text NOT NULL,
	"phone" text,
	"status" "user_status" DEFAULT 'ACTIVE' NOT NULL,
	"is_tenant_admin" boolean DEFAULT false NOT NULL,
	"storer_id" uuid,
	"locale" text DEFAULT 'en' NOT NULL,
	"last_login_at" timestamp with time zone,
	"failed_login_attempts" integer DEFAULT 0 NOT NULL,
	"locked_until" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "allocation_rules" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid,
	"sku_id" uuid,
	"name" text NOT NULL,
	"priority" integer DEFAULT 100 NOT NULL,
	"rotation" "rotation_rule" DEFAULT 'FIFO' NOT NULL,
	"pick_face_first" boolean DEFAULT true NOT NULL,
	"full_lpn_first" boolean DEFAULT true NOT NULL,
	"allow_partial_lpn" boolean DEFAULT true NOT NULL,
	"min_shelf_life_days" integer,
	"allowed_statuses" text[] DEFAULT '{"AVAILABLE"}' NOT NULL,
	"zone_ids" uuid[] DEFAULT '{}' NOT NULL,
	"active" boolean DEFAULT true 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 "carriers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"service_levels" text[] DEFAULT '{}' NOT NULL,
	"contacts" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"status" "active_status" DEFAULT 'ACTIVE' 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 "customers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"address" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"contacts" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"status" "active_status" DEFAULT 'ACTIVE' 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 "document_sequences" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid,
	"doc_type" text NOT NULL,
	"prefix" text NOT NULL,
	"next_number" integer DEFAULT 1 NOT NULL,
	"padding" integer DEFAULT 6 NOT NULL,
	CONSTRAINT "document_sequences_uq" UNIQUE NULLS NOT DISTINCT("tenant_id","warehouse_id","doc_type")
);
--> statement-breakpoint
CREATE TABLE "locations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"zone_id" uuid NOT NULL,
	"code" text NOT NULL,
	"barcode" text NOT NULL,
	"check_digit" text,
	"aisle" text,
	"bay" text,
	"level" text,
	"position" text,
	"type" "location_type" DEFAULT 'PALLET_RACK' NOT NULL,
	"usage" "location_usage" DEFAULT 'RESERVE' NOT NULL,
	"status" "location_status" DEFAULT 'ACTIVE' NOT NULL,
	"abc_class" text,
	"length_cm" numeric(10, 2),
	"width_cm" numeric(10, 2),
	"height_cm" numeric(10, 2),
	"cubic_capacity_m3" numeric(18, 6),
	"weight_capacity_kg" numeric(18, 4),
	"max_lpns" integer,
	"stack_limit" integer,
	"allow_mixed_sku" boolean DEFAULT true NOT NULL,
	"allow_mixed_lots" boolean DEFAULT true NOT NULL,
	"pick_sequence" integer DEFAULT 0 NOT NULL,
	"putaway_sequence" integer DEFAULT 0 NOT NULL,
	"x" numeric(10, 2),
	"y" numeric(10, 2),
	"attributes" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"last_counted_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "putaway_rules" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"name" text NOT NULL,
	"priority" integer DEFAULT 100 NOT NULL,
	"criteria" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"strategy" "putaway_strategy" DEFAULT 'EMPTY_LOCATION' NOT NULL,
	"target_zone_ids" uuid[] DEFAULT '{}' NOT NULL,
	"allow_mixed_sku" boolean DEFAULT true NOT NULL,
	"allow_mixed_lots" boolean DEFAULT false NOT NULL,
	"respect_capacity" boolean DEFAULT true NOT NULL,
	"active" boolean DEFAULT true 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 "reason_codes" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"type" "reason_code_type" NOT NULL,
	"code" text NOT NULL,
	"description" text NOT NULL,
	"requires_approval" boolean DEFAULT false NOT NULL,
	"requires_photo" boolean DEFAULT false NOT NULL,
	"active" boolean DEFAULT true NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sku_barcodes" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"sku_id" uuid NOT NULL,
	"barcode" text NOT NULL,
	"pack_level" "pack_level" DEFAULT 'EACH' NOT NULL,
	"is_primary" boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sku_packs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"sku_id" uuid NOT NULL,
	"level" "pack_level" NOT NULL,
	"uom_code" text NOT NULL,
	"qty_in_base" numeric(18, 4) NOT NULL,
	"length_cm" numeric(10, 2),
	"width_cm" numeric(10, 2),
	"height_cm" numeric(10, 2),
	"weight_kg" numeric(18, 4),
	"barcode" text,
	"pallet_ti" integer,
	"pallet_hi" integer,
	"is_default_receiving" boolean DEFAULT false NOT NULL,
	"is_default_shipping" boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sku_slottings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"sku_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"pick_location_id" uuid NOT NULL,
	"min_qty" numeric(18, 4) NOT NULL,
	"max_qty" numeric(18, 4) NOT NULL,
	"replenish_pack_level" "pack_level" DEFAULT 'CASE' NOT NULL,
	"active" boolean DEFAULT true 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 "skus" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"code" text NOT NULL,
	"description" text NOT NULL,
	"long_description" text,
	"category" text,
	"brand" text,
	"group_code" text,
	"material_type" text,
	"storage_condition" "storage_condition" DEFAULT 'AMBIENT' NOT NULL,
	"rotation_rule" "rotation_rule" DEFAULT 'FIFO' NOT NULL,
	"lot_controlled" boolean DEFAULT false NOT NULL,
	"expiry_controlled" boolean DEFAULT false NOT NULL,
	"serial_controlled" boolean DEFAULT false NOT NULL,
	"capture_serial_on_receipt" boolean DEFAULT true NOT NULL,
	"shelf_life_days" integer,
	"min_shelf_life_inbound_days" integer,
	"min_shelf_life_outbound_days" integer,
	"base_uom" text DEFAULT 'EA' NOT NULL,
	"weight_kg" numeric(18, 4),
	"cbm" numeric(18, 6),
	"length_cm" numeric(10, 2),
	"width_cm" numeric(10, 2),
	"height_cm" numeric(10, 2),
	"hs_code" text,
	"country_of_origin" text,
	"hazmat" boolean DEFAULT false NOT NULL,
	"hazmat_class" text,
	"abc_class" text,
	"count_frequency_days" integer,
	"unit_value" numeric(18, 4),
	"status" "active_status" DEFAULT 'ACTIVE' NOT NULL,
	"attributes" jsonb DEFAULT '{}'::jsonb 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 "storer_warehouses" (
	"storer_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	CONSTRAINT "storer_warehouses_storer_id_warehouse_id_pk" PRIMARY KEY("storer_id","warehouse_id")
);
--> statement-breakpoint
CREATE TABLE "storers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"legal_name" text,
	"tax_id" text,
	"address" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"contacts" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"status" "active_status" DEFAULT 'ACTIVE' NOT NULL,
	"settings" jsonb DEFAULT '{}'::jsonb 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 "suppliers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"address" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"contacts" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"status" "active_status" DEFAULT 'ACTIVE' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "suppliers_tenant_storer_code_uq" UNIQUE NULLS NOT DISTINCT("tenant_id","storer_id","code")
);
--> statement-breakpoint
CREATE TABLE "warehouses" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"address" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"timezone" text DEFAULT 'Asia/Kuwait' NOT NULL,
	"status" "active_status" DEFAULT 'ACTIVE' NOT NULL,
	"settings" jsonb DEFAULT '{}'::jsonb 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 "zones" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"type" "zone_type" DEFAULT 'STORAGE' NOT NULL,
	"temperature_class" "temperature_class" DEFAULT 'AMBIENT' NOT NULL,
	"pick_sequence" integer DEFAULT 100 NOT NULL,
	"status" "active_status" DEFAULT 'ACTIVE' 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 "asn_lines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"asn_id" uuid NOT NULL,
	"line_no" integer NOT NULL,
	"sku_id" uuid NOT NULL,
	"expected_qty" numeric(18, 4) NOT NULL,
	"pack_level" "pack_level" DEFAULT 'EACH' NOT NULL,
	"pack_qty" numeric(18, 4),
	"expected_lot" text,
	"expected_expiry" date,
	"received_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"damaged_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"rejected_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"status" "line_status" DEFAULT 'OPEN' NOT NULL,
	"external_line_ref" text,
	"unit_value" numeric(18, 4),
	"notes" text,
	"attributes" jsonb DEFAULT '{}'::jsonb NOT NULL
);
--> statement-breakpoint
CREATE TABLE "asns" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"asn_number" text NOT NULL,
	"external_ref" text,
	"type" "asn_type" DEFAULT 'PURCHASE' NOT NULL,
	"status" "asn_status" DEFAULT 'EXPECTED' NOT NULL,
	"source" "doc_source" DEFAULT 'MANUAL' NOT NULL,
	"supplier_id" uuid,
	"priority" integer DEFAULT 5 NOT NULL,
	"expected_at" timestamp with time zone,
	"carrier_name" text,
	"vehicle_no" text,
	"container_no" text,
	"container_size" text,
	"seal_no" text,
	"seal_intact" text,
	"driver_name" text,
	"driver_phone" text,
	"dock_location_id" uuid,
	"arrived_at" timestamp with time zone,
	"started_at" timestamp with time zone,
	"received_at" timestamp with time zone,
	"closed_at" timestamp with time zone,
	"cancelled_at" timestamp with time zone,
	"temperature_on_arrival" numeric(6, 2),
	"documents" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"notes" text,
	"attributes" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"total_expected_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"total_received_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"total_cbm" numeric(18, 6),
	"total_weight_kg" numeric(18, 4),
	"total_pallets" integer,
	"created_by_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "dock_appointments" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"dock_location_id" uuid,
	"direction" text NOT NULL,
	"ref_type" text,
	"ref_id" uuid,
	"starts_at" timestamp with time zone NOT NULL,
	"ends_at" timestamp with time zone NOT NULL,
	"status" "dock_appointment_status" DEFAULT 'BOOKED' NOT NULL,
	"carrier_name" text,
	"vehicle_no" text,
	"driver_name" text,
	"notes" text,
	"created_by_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "grns" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"asn_id" uuid NOT NULL,
	"grn_number" text NOT NULL,
	"posted_at" timestamp with time zone DEFAULT now() NOT NULL,
	"posted_by_id" uuid NOT NULL,
	"summary" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"document_id" uuid,
	"rcr_document_id" uuid,
	"rcr_sent_at" timestamp with time zone,
	CONSTRAINT "grns_asn_id_unique" UNIQUE("asn_id")
);
--> statement-breakpoint
CREATE TABLE "receipt_lines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"asn_id" uuid NOT NULL,
	"asn_line_id" uuid,
	"sku_id" uuid NOT NULL,
	"lpn_id" uuid,
	"location_id" uuid NOT NULL,
	"qty" numeric(18, 4) NOT NULL,
	"pack_level" "pack_level" DEFAULT 'EACH' NOT NULL,
	"pack_qty" numeric(18, 4),
	"lot_number" text DEFAULT '' NOT NULL,
	"expiry_date" date,
	"mfg_date" date,
	"condition" "item_condition" DEFAULT 'GOOD' NOT NULL,
	"reason_code_id" uuid,
	"serials" text[] DEFAULT '{}' NOT NULL,
	"movement_id" uuid,
	"received_by_id" uuid NOT NULL,
	"received_at" timestamp with time zone DEFAULT now() NOT NULL,
	"device_id" text,
	"notes" text,
	"reversed_at" timestamp with time zone,
	"reversed_by_id" uuid,
	"reversal_movement_id" uuid
);
--> statement-breakpoint
CREATE TABLE "receiving_discrepancies" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"asn_id" uuid NOT NULL,
	"asn_line_id" uuid,
	"sku_id" uuid NOT NULL,
	"type" "discrepancy_type" NOT NULL,
	"expected_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"actual_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"variance_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"status" "discrepancy_status" DEFAULT 'OPEN' NOT NULL,
	"resolution" text,
	"reported_at" timestamp with time zone,
	"resolved_at" timestamp with time zone,
	"resolved_by_id" uuid,
	"notes" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "cycle_count_lines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"count_id" uuid NOT NULL,
	"location_id" uuid NOT NULL,
	"sku_id" uuid,
	"storer_id" uuid,
	"lpn_id" uuid,
	"lot_number" text DEFAULT '' NOT NULL,
	"expiry_date" date,
	"inventory_status" "inventory_status" DEFAULT 'AVAILABLE' NOT NULL,
	"system_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"counted_qty" numeric(18, 4),
	"recount_qty" numeric(18, 4),
	"final_qty" numeric(18, 4),
	"variance_qty" numeric(18, 4),
	"status" "count_line_status" DEFAULT 'PENDING' NOT NULL,
	"counted_by_id" uuid,
	"counted_at" timestamp with time zone,
	"recount_by_id" uuid,
	"recount_at" timestamp with time zone,
	"approved_by_id" uuid,
	"adjustment_id" uuid,
	"notes" text
);
--> statement-breakpoint
CREATE TABLE "cycle_counts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"count_number" text NOT NULL,
	"type" "count_type" DEFAULT 'CYCLE' NOT NULL,
	"status" "count_status" DEFAULT 'PLANNED' NOT NULL,
	"storer_id" uuid,
	"scope" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"blind" boolean DEFAULT true NOT NULL,
	"recount_tolerance_pct" numeric(6, 3) DEFAULT 0 NOT NULL,
	"scheduled_for" timestamp with time zone,
	"released_at" timestamp with time zone,
	"completed_at" timestamp with time zone,
	"created_by_id" uuid NOT NULL,
	"notes" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "inventory_adjustment_lines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"adjustment_id" uuid NOT NULL,
	"sku_id" uuid NOT NULL,
	"location_id" uuid NOT NULL,
	"lpn_id" uuid,
	"lot_number" text DEFAULT '' NOT NULL,
	"expiry_date" date,
	"from_status" "inventory_status" DEFAULT 'AVAILABLE' NOT NULL,
	"to_status" "inventory_status",
	"qty_delta" numeric(18, 4) NOT NULL,
	"serials" text[] DEFAULT '{}' NOT NULL,
	"movement_id" uuid,
	"notes" text
);
--> statement-breakpoint
CREATE TABLE "inventory_adjustments" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"adj_number" text NOT NULL,
	"status" "approval_status" DEFAULT 'DRAFT' NOT NULL,
	"reason_code_id" uuid NOT NULL,
	"source" text DEFAULT 'MANUAL' NOT NULL,
	"count_id" uuid,
	"notes" text,
	"requested_by_id" uuid NOT NULL,
	"approved_by_id" uuid,
	"approved_at" timestamp with time zone,
	"rejected_reason" text,
	"posted_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "inventory_balances" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"sku_id" uuid NOT NULL,
	"location_id" uuid NOT NULL,
	"lpn_id" uuid,
	"lot_number" text DEFAULT '' NOT NULL,
	"expiry_date" date,
	"mfg_date" date,
	"status" "inventory_status" DEFAULT 'AVAILABLE' NOT NULL,
	"balance_key" text NOT NULL,
	"qty_on_hand" numeric(18, 4) DEFAULT 0 NOT NULL,
	"qty_allocated" numeric(18, 4) DEFAULT 0 NOT NULL,
	"first_received_at" timestamp with time zone DEFAULT now() NOT NULL,
	"last_moved_at" timestamp with time zone DEFAULT now() NOT NULL,
	"origin_asn_id" uuid,
	"unit_cost" numeric(18, 4),
	"attributes" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"version" integer DEFAULT 0 NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "inventory_balances_balance_key_unique" UNIQUE("balance_key")
);
--> statement-breakpoint
CREATE TABLE "inventory_holds" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"scope" "hold_scope" NOT NULL,
	"target_id" text NOT NULL,
	"storer_id" uuid,
	"sku_id" uuid,
	"reason_code_id" uuid,
	"to_status" "inventory_status" DEFAULT 'HOLD' NOT NULL,
	"affected_balance_ids" uuid[] DEFAULT '{}' NOT NULL,
	"notes" text,
	"placed_by_id" uuid NOT NULL,
	"placed_at" timestamp with time zone DEFAULT now() NOT NULL,
	"released_by_id" uuid,
	"released_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "inventory_movements" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"sku_id" uuid NOT NULL,
	"type" "movement_type" NOT NULL,
	"qty" numeric(18, 4) NOT NULL,
	"from_location_id" uuid,
	"to_location_id" uuid,
	"from_lpn_id" uuid,
	"to_lpn_id" uuid,
	"lot_number" text DEFAULT '' NOT NULL,
	"expiry_date" date,
	"from_status" "inventory_status",
	"to_status" "inventory_status",
	"ref_type" text,
	"ref_id" uuid,
	"task_id" uuid,
	"reason_code_id" uuid,
	"serials" text[] DEFAULT '{}' NOT NULL,
	"cbm" numeric(18, 6),
	"weight_kg" numeric(18, 4),
	"idempotency_key" text,
	"performed_by_id" uuid,
	"performed_at" timestamp with time zone DEFAULT now() NOT NULL,
	"device_id" text,
	"notes" text
);
--> statement-breakpoint
CREATE TABLE "lpns" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"lpn_number" text NOT NULL,
	"type" "lpn_type" DEFAULT 'PALLET' NOT NULL,
	"status" "lpn_status" DEFAULT 'ACTIVE' NOT NULL,
	"location_id" uuid,
	"parent_lpn_id" uuid,
	"storer_id" uuid,
	"weight_kg" numeric(18, 4),
	"height_cm" numeric(10, 2),
	"sealed" boolean DEFAULT false NOT NULL,
	"attributes" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"last_moved_at" timestamp with time zone,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "serial_units" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"sku_id" uuid NOT NULL,
	"serial_number" text NOT NULL,
	"status" serial_status DEFAULT 'IN_STOCK' NOT NULL,
	"warehouse_id" uuid,
	"location_id" uuid,
	"lpn_id" uuid,
	"lot_number" text DEFAULT '' NOT NULL,
	"expiry_date" date,
	"received_asn_id" uuid,
	"order_id" uuid,
	"last_movement_id" uuid,
	"attributes" jsonb DEFAULT '{}'::jsonb 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 "tasks" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"task_number" text NOT NULL,
	"type" "task_type" NOT NULL,
	"status" "task_status" DEFAULT 'PENDING' NOT NULL,
	"priority" integer DEFAULT 5 NOT NULL,
	"sequence" integer DEFAULT 0 NOT NULL,
	"group_key" text,
	"zone_id" uuid,
	"storer_id" uuid,
	"sku_id" uuid,
	"qty" numeric(18, 4),
	"qty_done" numeric(18, 4) DEFAULT 0 NOT NULL,
	"pack_level" "pack_level",
	"from_location_id" uuid,
	"to_location_id" uuid,
	"lpn_id" uuid,
	"to_lpn_id" uuid,
	"lot_number" text,
	"expiry_date" date,
	"ref_type" text,
	"ref_id" uuid,
	"assigned_to_id" uuid,
	"assigned_at" timestamp with time zone,
	"started_at" timestamp with time zone,
	"completed_at" timestamp with time zone,
	"completed_by_id" uuid,
	"exception_code" text,
	"exception_notes" text,
	"data" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"created_by_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "allocations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"order_line_id" uuid NOT NULL,
	"order_id" uuid NOT NULL,
	"balance_id" uuid NOT NULL,
	"sku_id" uuid NOT NULL,
	"location_id" uuid NOT NULL,
	"lpn_id" uuid,
	"lot_number" text DEFAULT '' NOT NULL,
	"expiry_date" date,
	"qty" numeric(18, 4) NOT NULL,
	"picked_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"status" "allocation_status" DEFAULT 'ALLOCATED' NOT NULL,
	"pick_task_id" uuid,
	"serials" text[] DEFAULT '{}' 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 "carton_lines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"carton_id" uuid NOT NULL,
	"order_line_id" uuid NOT NULL,
	"sku_id" uuid NOT NULL,
	"qty" numeric(18, 4) NOT NULL,
	"lot_number" text DEFAULT '' NOT NULL,
	"expiry_date" date,
	"serials" text[] DEFAULT '{}' NOT NULL
);
--> statement-breakpoint
CREATE TABLE "cartons" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"shipment_id" uuid NOT NULL,
	"carton_number" text NOT NULL,
	"type" text DEFAULT 'CARTON' NOT NULL,
	"lpn_id" uuid,
	"weight_kg" numeric(18, 4),
	"length_cm" numeric(10, 2),
	"width_cm" numeric(10, 2),
	"height_cm" numeric(10, 2),
	"tracking_no" text,
	"sealed" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "loads" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"load_number" text NOT NULL,
	"status" "load_status" DEFAULT 'PLANNED' NOT NULL,
	"carrier_id" uuid,
	"carrier_name" text,
	"vehicle_no" text,
	"vehicle_type" text,
	"driver_name" text,
	"driver_id_no" text,
	"driver_phone" text,
	"seal_no" text,
	"dock_location_id" uuid,
	"scheduled_at" timestamp with time zone,
	"loading_started_at" timestamp with time zone,
	"loaded_at" timestamp with time zone,
	"departed_at" timestamp with time zone,
	"delivered_at" timestamp with time zone,
	"gate_pass_number" text,
	"gate_pass_document_id" uuid,
	"temperature_at_dispatch" numeric(6, 2),
	"pod" jsonb,
	"notes" text,
	"created_by_id" uuid 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 "outbound_order_lines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"order_id" uuid NOT NULL,
	"line_no" integer NOT NULL,
	"sku_id" uuid NOT NULL,
	"ordered_qty" numeric(18, 4) NOT NULL,
	"pack_level" "pack_level" DEFAULT 'EACH' NOT NULL,
	"pack_qty" numeric(18, 4),
	"allocated_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"picked_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"packed_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"shipped_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"cancelled_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"requested_lot" text,
	"requested_serials" text[] DEFAULT '{}' NOT NULL,
	"min_expiry_date" date,
	"status" "line_status" DEFAULT 'OPEN' NOT NULL,
	"external_line_ref" text,
	"unit_price" numeric(18, 4),
	"notes" text,
	"attributes" jsonb DEFAULT '{}'::jsonb NOT NULL
);
--> statement-breakpoint
CREATE TABLE "outbound_orders" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"order_number" text NOT NULL,
	"external_ref" text,
	"type" "order_type" DEFAULT 'SALES' NOT NULL,
	"status" "order_status" DEFAULT 'OPEN' NOT NULL,
	"source" "doc_source" DEFAULT 'MANUAL' NOT NULL,
	"priority" integer DEFAULT 5 NOT NULL,
	"customer_id" uuid,
	"ship_to" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"carrier_id" uuid,
	"carrier_name" text,
	"service_level" text,
	"requested_ship_date" date,
	"due_date" timestamp with time zone,
	"wave_id" uuid,
	"load_id" uuid,
	"hold_reason" text,
	"notes" text,
	"attributes" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"total_ordered_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"total_shipped_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"total_cbm" numeric(18, 6),
	"total_weight_kg" numeric(18, 4),
	"allocated_at" timestamp with time zone,
	"released_at" timestamp with time zone,
	"picked_at" timestamp with time zone,
	"packed_at" timestamp with time zone,
	"shipped_at" timestamp with time zone,
	"cancelled_at" timestamp with time zone,
	"created_by_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "return_order_lines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"return_id" uuid NOT NULL,
	"line_no" integer NOT NULL,
	"sku_id" uuid NOT NULL,
	"expected_qty" numeric(18, 4) NOT NULL,
	"received_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"disposition" "return_disposition",
	"reason_code_id" uuid,
	"serials" text[] DEFAULT '{}' NOT NULL,
	"notes" text
);
--> statement-breakpoint
CREATE TABLE "return_orders" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"rma_number" text NOT NULL,
	"original_order_id" uuid,
	"status" "return_status" DEFAULT 'REQUESTED' NOT NULL,
	"reason_code_id" uuid,
	"asn_id" uuid,
	"customer_ref" text,
	"notes" text,
	"created_by_id" uuid 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 "shipments" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"order_id" uuid NOT NULL,
	"shipment_number" text NOT NULL,
	"status" "shipment_status" DEFAULT 'PACKING' NOT NULL,
	"packing_location_id" uuid,
	"staging_location_id" uuid,
	"load_id" uuid,
	"carrier_id" uuid,
	"tracking_no" text,
	"carton_count" integer DEFAULT 0 NOT NULL,
	"pallet_count" integer DEFAULT 0 NOT NULL,
	"total_weight_kg" numeric(18, 4),
	"total_cbm" numeric(18, 6),
	"dn_number" text,
	"dn_document_id" uuid,
	"packing_list_document_id" uuid,
	"packed_at" timestamp with time zone,
	"staged_at" timestamp with time zone,
	"loaded_at" timestamp with time zone,
	"shipped_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "waves" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"wave_number" text NOT NULL,
	"status" "wave_status" DEFAULT 'PLANNED' NOT NULL,
	"strategy" "wave_strategy" DEFAULT 'BY_ORDER' NOT NULL,
	"criteria" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"order_count" integer DEFAULT 0 NOT NULL,
	"line_count" integer DEFAULT 0 NOT NULL,
	"task_count" integer DEFAULT 0 NOT NULL,
	"short_lines" integer DEFAULT 0 NOT NULL,
	"created_by_id" uuid NOT NULL,
	"allocated_at" timestamp with time zone,
	"released_at" timestamp with time zone,
	"completed_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "billing_contracts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"contract_number" text NOT NULL,
	"name" text NOT NULL,
	"currency" text DEFAULT 'KWD' NOT NULL,
	"status" "contract_status" DEFAULT 'DRAFT' NOT NULL,
	"billing_cycle" "billing_cycle" DEFAULT 'MONTHLY' NOT NULL,
	"storage_method" "storage_method" DEFAULT 'WEEKLY_CBM_OPENING_PLUS_IN' NOT NULL,
	"storage_cap" jsonb,
	"minimum_monthly" numeric(18, 4),
	"fixed_monthly_fee" numeric(18, 4),
	"payment_terms_days" integer DEFAULT 30 NOT NULL,
	"week_starts_on" integer DEFAULT 1 NOT NULL,
	"valid_from" date NOT NULL,
	"valid_to" date,
	"tax_rate_pct" numeric(6, 3) DEFAULT 0 NOT NULL,
	"notes" text,
	"created_by_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "charge_events" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"contract_id" uuid NOT NULL,
	"tariff_id" uuid NOT NULL,
	"ref_type" text,
	"ref_id" uuid,
	"ref_number" text,
	"description" text NOT NULL,
	"quantity" numeric(18, 4) NOT NULL,
	"unit" charge_unit NOT NULL,
	"rate" numeric(18, 6) NOT NULL,
	"amount" numeric(18, 4) NOT NULL,
	"currency" text NOT NULL,
	"charge_date" date NOT NULL,
	"period_key" text NOT NULL,
	"status" charge_status DEFAULT 'PENDING' NOT NULL,
	"source" "doc_source" DEFAULT 'SYSTEM' NOT NULL,
	"invoice_line_id" uuid,
	"detail" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"idempotency_key" text,
	"waived_reason" text,
	"created_by_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "credit_notes" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"invoice_id" uuid NOT NULL,
	"credit_number" text NOT NULL,
	"amount" numeric(18, 4) NOT NULL,
	"reason" text NOT NULL,
	"status" text DEFAULT 'ISSUED' NOT NULL,
	"issued_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by_id" uuid
);
--> statement-breakpoint
CREATE TABLE "invoice_lines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"invoice_id" uuid NOT NULL,
	"tariff_id" uuid,
	"group" "tariff_group" NOT NULL,
	"description" text NOT NULL,
	"quantity" numeric(18, 4) NOT NULL,
	"unit" charge_unit NOT NULL,
	"rate" numeric(18, 6) NOT NULL,
	"amount" numeric(18, 4) NOT NULL,
	"tax_rate_pct" numeric(6, 3) DEFAULT 0 NOT NULL,
	"tax_amount" numeric(18, 4) DEFAULT 0 NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"detail" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"charge_event_ids" uuid[] DEFAULT '{}' NOT NULL
);
--> statement-breakpoint
CREATE TABLE "invoices" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"contract_id" uuid NOT NULL,
	"invoice_number" text NOT NULL,
	"period_start" date NOT NULL,
	"period_end" date NOT NULL,
	"status" "invoice_status" DEFAULT 'DRAFT' NOT NULL,
	"currency" text NOT NULL,
	"subtotal" numeric(18, 4) DEFAULT 0 NOT NULL,
	"tax_total" numeric(18, 4) DEFAULT 0 NOT NULL,
	"total" numeric(18, 4) DEFAULT 0 NOT NULL,
	"paid_amount" numeric(18, 4) DEFAULT 0 NOT NULL,
	"issued_at" timestamp with time zone,
	"due_at" date,
	"sent_at" timestamp with time zone,
	"paid_at" timestamp with time zone,
	"document_id" uuid,
	"notes" text,
	"created_by_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "storage_snapshots" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid NOT NULL,
	"storer_id" uuid NOT NULL,
	"snapshot_date" date NOT NULL,
	"total_qty" numeric(18, 4) DEFAULT 0 NOT NULL,
	"total_cbm" numeric(18, 6) DEFAULT 0 NOT NULL,
	"total_weight_kg" numeric(18, 4) DEFAULT 0 NOT NULL,
	"pallet_positions" integer DEFAULT 0 NOT NULL,
	"inbound_cbm" numeric(18, 6) DEFAULT 0 NOT NULL,
	"outbound_cbm" numeric(18, 6) DEFAULT 0 NOT NULL,
	"by_temperature_class" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "tariffs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"contract_id" uuid NOT NULL,
	"code" text NOT NULL,
	"group" "tariff_group" NOT NULL,
	"description" text NOT NULL,
	"charge_unit" charge_unit NOT NULL,
	"rate" numeric(18, 6) NOT NULL,
	"minimum_charge" numeric(18, 4),
	"tiers" jsonb,
	"trigger" charge_trigger DEFAULT 'MANUAL' NOT NULL,
	"conditions" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"taxable" boolean DEFAULT true NOT NULL,
	"valid_from" date,
	"valid_to" date,
	"active" boolean DEFAULT true NOT NULL,
	"sort_order" integer DEFAULT 100 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 "import_jobs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid,
	"storer_id" uuid,
	"type" text NOT NULL,
	"file_name" text NOT NULL,
	"status" text DEFAULT 'PENDING' NOT NULL,
	"dry_run" boolean DEFAULT false NOT NULL,
	"total_rows" integer DEFAULT 0 NOT NULL,
	"success_rows" integer DEFAULT 0 NOT NULL,
	"error_rows" integer DEFAULT 0 NOT NULL,
	"errors" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"result_refs" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"created_by_id" uuid NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"completed_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "integration_connectors" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid,
	"type" "connector_type" NOT NULL,
	"name" text NOT NULL,
	"config" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"active" boolean DEFAULT true 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 "integration_messages" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid,
	"connector_id" uuid,
	"direction" "message_direction" NOT NULL,
	"message_type" text NOT NULL,
	"external_id" text,
	"idempotency_key" text,
	"payload" jsonb NOT NULL,
	"raw_payload" text,
	"status" "message_status" DEFAULT 'RECEIVED' NOT NULL,
	"attempts" integer DEFAULT 0 NOT NULL,
	"last_error" text,
	"result_ref_type" text,
	"result_ref_id" uuid,
	"processed_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "webhook_deliveries" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"subscription_id" uuid NOT NULL,
	"event_type" text NOT NULL,
	"event_id" uuid NOT NULL,
	"payload" jsonb NOT NULL,
	"status" text DEFAULT 'PENDING' NOT NULL,
	"attempts" integer DEFAULT 0 NOT NULL,
	"response_code" integer,
	"response_body" text,
	"next_attempt_at" timestamp with time zone,
	"delivered_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "webhook_subscriptions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"storer_id" uuid,
	"name" text NOT NULL,
	"url" text NOT NULL,
	"secret_encrypted" text NOT NULL,
	"events" text[] DEFAULT '{}' NOT NULL,
	"active" boolean DEFAULT true 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 "ai_conversations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"warehouse_id" uuid,
	"title" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "ai_messages" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"conversation_id" uuid NOT NULL,
	"role" text NOT NULL,
	"content" text NOT NULL,
	"tool_calls" jsonb,
	"prompt_tokens" integer DEFAULT 0 NOT NULL,
	"completion_tokens" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "ai_proposals" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"warehouse_id" uuid,
	"storer_id" uuid,
	"type" "ai_proposal_type" NOT NULL,
	"status" "ai_proposal_status" DEFAULT 'PROPOSED' NOT NULL,
	"title" text NOT NULL,
	"summary" text NOT NULL,
	"payload" jsonb NOT NULL,
	"evidence" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"confidence" numeric(6, 3),
	"severity" text,
	"model" text,
	"prompt_tokens" integer DEFAULT 0 NOT NULL,
	"completion_tokens" integer DEFAULT 0 NOT NULL,
	"requested_by_id" uuid,
	"decided_by_id" uuid,
	"decided_at" timestamp with time zone,
	"decision_notes" text,
	"applied_ref_type" text,
	"applied_ref_id" uuid,
	"expires_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "attachments" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"ref_type" text NOT NULL,
	"ref_id" uuid NOT NULL,
	"file_name" text NOT NULL,
	"mime_type" text NOT NULL,
	"size_bytes" integer NOT NULL,
	"storage_key" text NOT NULL,
	"kind" text DEFAULT 'PHOTO' NOT NULL,
	"uploaded_by_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "audit_logs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid,
	"actor_type" "actor_type" NOT NULL,
	"actor_id" text,
	"actor_name" text,
	"action" text NOT NULL,
	"entity_type" text,
	"entity_id" text,
	"before" jsonb,
	"after" jsonb,
	"ip" text,
	"user_agent" text,
	"request_id" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "documents" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"type" text NOT NULL,
	"number" text NOT NULL,
	"ref_type" text NOT NULL,
	"ref_id" uuid NOT NULL,
	"mime_type" text DEFAULT 'application/pdf' NOT NULL,
	"storage_key" text NOT NULL,
	"size_bytes" integer,
	"generated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"generated_by_id" uuid,
	"sent_at" timestamp with time zone,
	"sent_to" text[] DEFAULT '{}' NOT NULL
);
--> statement-breakpoint
CREATE TABLE "email_outbox" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"to" text[] NOT NULL,
	"cc" text[] DEFAULT '{}' NOT NULL,
	"subject" text NOT NULL,
	"body_html" text NOT NULL,
	"attachments" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"ref_type" text,
	"ref_id" uuid,
	"status" text DEFAULT 'PENDING' NOT NULL,
	"attempts" integer DEFAULT 0 NOT NULL,
	"last_error" text,
	"sent_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "notifications" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"type" text NOT NULL,
	"title" text NOT NULL,
	"body" text,
	"data" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"read_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "outbox_events" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"type" text NOT NULL,
	"warehouse_id" uuid,
	"storer_id" uuid,
	"actor_id" uuid,
	"payload" jsonb NOT NULL,
	"status" text DEFAULT 'PENDING' NOT NULL,
	"attempts" integer DEFAULT 0 NOT NULL,
	"last_error" text,
	"processed_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "feature_flags" ADD CONSTRAINT "feature_flags_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_plan_id_plans_id_fk" FOREIGN KEY ("plan_id") REFERENCES "public"."plans"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tenant_settings" ADD CONSTRAINT "tenant_settings_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "usage_records" ADD CONSTRAINT "usage_records_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "api_keys" ADD CONSTRAINT "api_keys_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "api_keys" ADD CONSTRAINT "api_keys_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "roles" ADD CONSTRAINT "roles_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("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 "user_roles" ADD CONSTRAINT "user_roles_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_roles" ADD CONSTRAINT "user_roles_role_id_roles_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."roles"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_roles" ADD CONSTRAINT "user_roles_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_warehouses" ADD CONSTRAINT "user_warehouses_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_warehouses" ADD CONSTRAINT "user_warehouses_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "users" ADD CONSTRAINT "users_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "users" ADD CONSTRAINT "users_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "allocation_rules" ADD CONSTRAINT "allocation_rules_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "allocation_rules" ADD CONSTRAINT "allocation_rules_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "customers" ADD CONSTRAINT "customers_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "locations" ADD CONSTRAINT "locations_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "locations" ADD CONSTRAINT "locations_zone_id_zones_id_fk" FOREIGN KEY ("zone_id") REFERENCES "public"."zones"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "putaway_rules" ADD CONSTRAINT "putaway_rules_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sku_barcodes" ADD CONSTRAINT "sku_barcodes_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sku_packs" ADD CONSTRAINT "sku_packs_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sku_slottings" ADD CONSTRAINT "sku_slottings_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sku_slottings" ADD CONSTRAINT "sku_slottings_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sku_slottings" ADD CONSTRAINT "sku_slottings_pick_location_id_locations_id_fk" FOREIGN KEY ("pick_location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "skus" ADD CONSTRAINT "skus_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "storer_warehouses" ADD CONSTRAINT "storer_warehouses_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "storer_warehouses" ADD CONSTRAINT "storer_warehouses_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "storers" ADD CONSTRAINT "storers_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "suppliers" ADD CONSTRAINT "suppliers_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "warehouses" ADD CONSTRAINT "warehouses_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "zones" ADD CONSTRAINT "zones_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asn_lines" ADD CONSTRAINT "asn_lines_asn_id_asns_id_fk" FOREIGN KEY ("asn_id") REFERENCES "public"."asns"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asn_lines" ADD CONSTRAINT "asn_lines_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asns" ADD CONSTRAINT "asns_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asns" ADD CONSTRAINT "asns_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asns" ADD CONSTRAINT "asns_supplier_id_suppliers_id_fk" FOREIGN KEY ("supplier_id") REFERENCES "public"."suppliers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asns" ADD CONSTRAINT "asns_dock_location_id_locations_id_fk" FOREIGN KEY ("dock_location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "dock_appointments" ADD CONSTRAINT "dock_appointments_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "dock_appointments" ADD CONSTRAINT "dock_appointments_dock_location_id_locations_id_fk" FOREIGN KEY ("dock_location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "grns" ADD CONSTRAINT "grns_asn_id_asns_id_fk" FOREIGN KEY ("asn_id") REFERENCES "public"."asns"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_asn_id_asns_id_fk" FOREIGN KEY ("asn_id") REFERENCES "public"."asns"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_asn_line_id_asn_lines_id_fk" FOREIGN KEY ("asn_line_id") REFERENCES "public"."asn_lines"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "receipt_lines" ADD CONSTRAINT "receipt_lines_location_id_locations_id_fk" FOREIGN KEY ("location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "receiving_discrepancies" ADD CONSTRAINT "receiving_discrepancies_asn_id_asns_id_fk" FOREIGN KEY ("asn_id") REFERENCES "public"."asns"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "receiving_discrepancies" ADD CONSTRAINT "receiving_discrepancies_asn_line_id_asn_lines_id_fk" FOREIGN KEY ("asn_line_id") REFERENCES "public"."asn_lines"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "receiving_discrepancies" ADD CONSTRAINT "receiving_discrepancies_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "cycle_count_lines" ADD CONSTRAINT "cycle_count_lines_count_id_cycle_counts_id_fk" FOREIGN KEY ("count_id") REFERENCES "public"."cycle_counts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "cycle_count_lines" ADD CONSTRAINT "cycle_count_lines_location_id_locations_id_fk" FOREIGN KEY ("location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "cycle_counts" ADD CONSTRAINT "cycle_counts_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_adjustment_lines" ADD CONSTRAINT "inventory_adjustment_lines_adjustment_id_inventory_adjustments_id_fk" FOREIGN KEY ("adjustment_id") REFERENCES "public"."inventory_adjustments"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_adjustment_lines" ADD CONSTRAINT "inventory_adjustment_lines_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_adjustment_lines" ADD CONSTRAINT "inventory_adjustment_lines_location_id_locations_id_fk" FOREIGN KEY ("location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_adjustments" ADD CONSTRAINT "inventory_adjustments_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_adjustments" ADD CONSTRAINT "inventory_adjustments_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_balances" ADD CONSTRAINT "inventory_balances_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_balances" ADD CONSTRAINT "inventory_balances_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_balances" ADD CONSTRAINT "inventory_balances_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_balances" ADD CONSTRAINT "inventory_balances_location_id_locations_id_fk" FOREIGN KEY ("location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_balances" ADD CONSTRAINT "inventory_balances_lpn_id_lpns_id_fk" FOREIGN KEY ("lpn_id") REFERENCES "public"."lpns"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lpns" ADD CONSTRAINT "lpns_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lpns" ADD CONSTRAINT "lpns_location_id_locations_id_fk" FOREIGN KEY ("location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lpns" ADD CONSTRAINT "lpns_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "serial_units" ADD CONSTRAINT "serial_units_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "serial_units" ADD CONSTRAINT "serial_units_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tasks" ADD CONSTRAINT "tasks_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "allocations" ADD CONSTRAINT "allocations_order_line_id_outbound_order_lines_id_fk" FOREIGN KEY ("order_line_id") REFERENCES "public"."outbound_order_lines"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "allocations" ADD CONSTRAINT "allocations_balance_id_inventory_balances_id_fk" FOREIGN KEY ("balance_id") REFERENCES "public"."inventory_balances"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "carton_lines" ADD CONSTRAINT "carton_lines_carton_id_cartons_id_fk" FOREIGN KEY ("carton_id") REFERENCES "public"."cartons"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "cartons" ADD CONSTRAINT "cartons_shipment_id_shipments_id_fk" FOREIGN KEY ("shipment_id") REFERENCES "public"."shipments"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loads" ADD CONSTRAINT "loads_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loads" ADD CONSTRAINT "loads_carrier_id_carriers_id_fk" FOREIGN KEY ("carrier_id") REFERENCES "public"."carriers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loads" ADD CONSTRAINT "loads_dock_location_id_locations_id_fk" FOREIGN KEY ("dock_location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "outbound_order_lines" ADD CONSTRAINT "outbound_order_lines_order_id_outbound_orders_id_fk" FOREIGN KEY ("order_id") REFERENCES "public"."outbound_orders"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "outbound_order_lines" ADD CONSTRAINT "outbound_order_lines_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "outbound_orders" ADD CONSTRAINT "outbound_orders_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "outbound_orders" ADD CONSTRAINT "outbound_orders_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "outbound_orders" ADD CONSTRAINT "outbound_orders_customer_id_customers_id_fk" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "outbound_orders" ADD CONSTRAINT "outbound_orders_carrier_id_carriers_id_fk" FOREIGN KEY ("carrier_id") REFERENCES "public"."carriers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "outbound_orders" ADD CONSTRAINT "outbound_orders_wave_id_waves_id_fk" FOREIGN KEY ("wave_id") REFERENCES "public"."waves"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "outbound_orders" ADD CONSTRAINT "outbound_orders_load_id_loads_id_fk" FOREIGN KEY ("load_id") REFERENCES "public"."loads"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "return_order_lines" ADD CONSTRAINT "return_order_lines_return_id_return_orders_id_fk" FOREIGN KEY ("return_id") REFERENCES "public"."return_orders"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "return_order_lines" ADD CONSTRAINT "return_order_lines_sku_id_skus_id_fk" FOREIGN KEY ("sku_id") REFERENCES "public"."skus"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "return_orders" ADD CONSTRAINT "return_orders_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "return_orders" ADD CONSTRAINT "return_orders_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "shipments" ADD CONSTRAINT "shipments_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "shipments" ADD CONSTRAINT "shipments_order_id_outbound_orders_id_fk" FOREIGN KEY ("order_id") REFERENCES "public"."outbound_orders"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "shipments" ADD CONSTRAINT "shipments_packing_location_id_locations_id_fk" FOREIGN KEY ("packing_location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "shipments" ADD CONSTRAINT "shipments_staging_location_id_locations_id_fk" FOREIGN KEY ("staging_location_id") REFERENCES "public"."locations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "shipments" ADD CONSTRAINT "shipments_load_id_loads_id_fk" FOREIGN KEY ("load_id") REFERENCES "public"."loads"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "waves" ADD CONSTRAINT "waves_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "billing_contracts" ADD CONSTRAINT "billing_contracts_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "charge_events" ADD CONSTRAINT "charge_events_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "charge_events" ADD CONSTRAINT "charge_events_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "charge_events" ADD CONSTRAINT "charge_events_tariff_id_tariffs_id_fk" FOREIGN KEY ("tariff_id") REFERENCES "public"."tariffs"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "credit_notes" ADD CONSTRAINT "credit_notes_invoice_id_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."invoices"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "invoice_lines" ADD CONSTRAINT "invoice_lines_invoice_id_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."invoices"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_contract_id_billing_contracts_id_fk" FOREIGN KEY ("contract_id") REFERENCES "public"."billing_contracts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "storage_snapshots" ADD CONSTRAINT "storage_snapshots_warehouse_id_warehouses_id_fk" FOREIGN KEY ("warehouse_id") REFERENCES "public"."warehouses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "storage_snapshots" ADD CONSTRAINT "storage_snapshots_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tariffs" ADD CONSTRAINT "tariffs_contract_id_billing_contracts_id_fk" FOREIGN KEY ("contract_id") REFERENCES "public"."billing_contracts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "integration_connectors" ADD CONSTRAINT "integration_connectors_storer_id_storers_id_fk" FOREIGN KEY ("storer_id") REFERENCES "public"."storers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "webhook_deliveries" ADD CONSTRAINT "webhook_deliveries_subscription_id_webhook_subscriptions_id_fk" FOREIGN KEY ("subscription_id") REFERENCES "public"."webhook_subscriptions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ai_messages" ADD CONSTRAINT "ai_messages_conversation_id_ai_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."ai_conversations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "feature_flags_tenant_key_uq" ON "feature_flags" USING btree ("tenant_id","key");--> statement-breakpoint
CREATE INDEX "subscriptions_tenant_status_idx" ON "subscriptions" USING btree ("tenant_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "tenant_settings_tenant_key_uq" ON "tenant_settings" USING btree ("tenant_id","key");--> statement-breakpoint
CREATE UNIQUE INDEX "usage_records_tenant_metric_date_uq" ON "usage_records" USING btree ("tenant_id","metric","usage_date");--> statement-breakpoint
CREATE INDEX "api_keys_tenant_idx" ON "api_keys" USING btree ("tenant_id");--> statement-breakpoint
CREATE UNIQUE INDEX "roles_tenant_code_uq" ON "roles" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE INDEX "sessions_user_idx" ON "sessions" USING btree ("user_id");--> statement-breakpoint
CREATE UNIQUE INDEX "users_tenant_email_uq" ON "users" USING btree ("tenant_id","email");--> statement-breakpoint
CREATE INDEX "users_tenant_status_idx" ON "users" USING btree ("tenant_id","status");--> statement-breakpoint
CREATE INDEX "allocation_rules_lookup_idx" ON "allocation_rules" USING btree ("tenant_id","active","priority");--> statement-breakpoint
CREATE UNIQUE INDEX "carriers_tenant_code_uq" ON "carriers" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE UNIQUE INDEX "customers_tenant_storer_code_uq" ON "customers" USING btree ("tenant_id","storer_id","code");--> statement-breakpoint
CREATE INDEX "customers_tenant_idx" ON "customers" USING btree ("tenant_id");--> statement-breakpoint
CREATE UNIQUE INDEX "locations_wh_code_uq" ON "locations" USING btree ("warehouse_id","code");--> statement-breakpoint
CREATE UNIQUE INDEX "locations_wh_barcode_uq" ON "locations" USING btree ("warehouse_id","barcode");--> statement-breakpoint
CREATE INDEX "locations_tenant_wh_zone_idx" ON "locations" USING btree ("tenant_id","warehouse_id","zone_id");--> statement-breakpoint
CREATE INDEX "locations_wh_usage_status_idx" ON "locations" USING btree ("warehouse_id","usage","status");--> statement-breakpoint
CREATE INDEX "putaway_rules_lookup_idx" ON "putaway_rules" USING btree ("tenant_id","warehouse_id","active","priority");--> statement-breakpoint
CREATE UNIQUE INDEX "reason_codes_uq" ON "reason_codes" USING btree ("tenant_id","type","code");--> statement-breakpoint
CREATE UNIQUE INDEX "sku_barcodes_tenant_barcode_uq" ON "sku_barcodes" USING btree ("tenant_id","barcode");--> statement-breakpoint
CREATE INDEX "sku_barcodes_sku_idx" ON "sku_barcodes" USING btree ("sku_id");--> statement-breakpoint
CREATE UNIQUE INDEX "sku_packs_sku_level_uq" ON "sku_packs" USING btree ("sku_id","level");--> statement-breakpoint
CREATE INDEX "sku_packs_tenant_barcode_idx" ON "sku_packs" USING btree ("tenant_id","barcode");--> statement-breakpoint
CREATE UNIQUE INDEX "sku_slottings_uq" ON "sku_slottings" USING btree ("sku_id","warehouse_id","pick_location_id");--> statement-breakpoint
CREATE INDEX "sku_slottings_tenant_wh_idx" ON "sku_slottings" USING btree ("tenant_id","warehouse_id");--> statement-breakpoint
CREATE UNIQUE INDEX "skus_tenant_storer_code_uq" ON "skus" USING btree ("tenant_id","storer_id","code");--> statement-breakpoint
CREATE INDEX "skus_tenant_storer_status_idx" ON "skus" USING btree ("tenant_id","storer_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "storers_tenant_code_uq" ON "storers" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE INDEX "suppliers_tenant_idx" ON "suppliers" USING btree ("tenant_id");--> statement-breakpoint
CREATE UNIQUE INDEX "warehouses_tenant_code_uq" ON "warehouses" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE UNIQUE INDEX "zones_wh_code_uq" ON "zones" USING btree ("warehouse_id","code");--> statement-breakpoint
CREATE INDEX "zones_tenant_idx" ON "zones" USING btree ("tenant_id");--> statement-breakpoint
CREATE UNIQUE INDEX "asn_lines_asn_lineno_uq" ON "asn_lines" USING btree ("asn_id","line_no");--> statement-breakpoint
CREATE INDEX "asn_lines_tenant_sku_idx" ON "asn_lines" USING btree ("tenant_id","sku_id");--> statement-breakpoint
CREATE UNIQUE INDEX "asns_tenant_number_uq" ON "asns" USING btree ("tenant_id","asn_number");--> statement-breakpoint
CREATE INDEX "asns_tenant_wh_status_idx" ON "asns" USING btree ("tenant_id","warehouse_id","status");--> statement-breakpoint
CREATE INDEX "asns_tenant_storer_ref_idx" ON "asns" USING btree ("tenant_id","storer_id","external_ref");--> statement-breakpoint
CREATE INDEX "dock_appointments_wh_start_idx" ON "dock_appointments" USING btree ("tenant_id","warehouse_id","starts_at");--> statement-breakpoint
CREATE UNIQUE INDEX "grns_tenant_number_uq" ON "grns" USING btree ("tenant_id","grn_number");--> statement-breakpoint
CREATE INDEX "receipt_lines_tenant_asn_idx" ON "receipt_lines" USING btree ("tenant_id","asn_id");--> statement-breakpoint
CREATE INDEX "receipt_lines_lpn_idx" ON "receipt_lines" USING btree ("lpn_id");--> statement-breakpoint
CREATE INDEX "receiving_discrepancies_asn_status_idx" ON "receiving_discrepancies" USING btree ("tenant_id","asn_id","status");--> statement-breakpoint
CREATE INDEX "cycle_count_lines_count_status_idx" ON "cycle_count_lines" USING btree ("tenant_id","count_id","status");--> statement-breakpoint
CREATE INDEX "cycle_count_lines_location_idx" ON "cycle_count_lines" USING btree ("tenant_id","location_id");--> statement-breakpoint
CREATE UNIQUE INDEX "cycle_counts_number_uq" ON "cycle_counts" USING btree ("tenant_id","count_number");--> statement-breakpoint
CREATE INDEX "cycle_counts_wh_status_idx" ON "cycle_counts" USING btree ("tenant_id","warehouse_id","status");--> statement-breakpoint
CREATE INDEX "inventory_adjustment_lines_adj_idx" ON "inventory_adjustment_lines" USING btree ("tenant_id","adjustment_id");--> statement-breakpoint
CREATE UNIQUE INDEX "inventory_adjustments_number_uq" ON "inventory_adjustments" USING btree ("tenant_id","adj_number");--> statement-breakpoint
CREATE INDEX "inventory_adjustments_wh_status_idx" ON "inventory_adjustments" USING btree ("tenant_id","warehouse_id","status");--> statement-breakpoint
CREATE INDEX "inv_bal_lookup_idx" ON "inventory_balances" USING btree ("tenant_id","warehouse_id","storer_id","sku_id","status");--> statement-breakpoint
CREATE INDEX "inv_bal_location_idx" ON "inventory_balances" USING btree ("tenant_id","location_id");--> statement-breakpoint
CREATE INDEX "inv_bal_lpn_idx" ON "inventory_balances" USING btree ("tenant_id","lpn_id");--> statement-breakpoint
CREATE INDEX "inv_bal_expiry_idx" ON "inventory_balances" USING btree ("tenant_id","expiry_date");--> statement-breakpoint
CREATE INDEX "inventory_holds_target_idx" ON "inventory_holds" USING btree ("tenant_id","warehouse_id","scope","target_id");--> statement-breakpoint
CREATE UNIQUE INDEX "inv_mov_idem_uq" ON "inventory_movements" USING btree ("tenant_id","idempotency_key");--> statement-breakpoint
CREATE INDEX "inv_mov_sku_time_idx" ON "inventory_movements" USING btree ("tenant_id","warehouse_id","storer_id","sku_id","performed_at");--> statement-breakpoint
CREATE INDEX "inv_mov_ref_idx" ON "inventory_movements" USING btree ("tenant_id","ref_type","ref_id");--> statement-breakpoint
CREATE INDEX "inv_mov_time_idx" ON "inventory_movements" USING btree ("tenant_id","performed_at");--> statement-breakpoint
CREATE UNIQUE INDEX "lpns_tenant_number_uq" ON "lpns" USING btree ("tenant_id","lpn_number");--> statement-breakpoint
CREATE INDEX "lpns_tenant_wh_loc_idx" ON "lpns" USING btree ("tenant_id","warehouse_id","location_id");--> statement-breakpoint
CREATE UNIQUE INDEX "serial_units_uq" ON "serial_units" USING btree ("tenant_id","storer_id","sku_id","serial_number");--> statement-breakpoint
CREATE INDEX "serial_units_status_wh_idx" ON "serial_units" USING btree ("tenant_id","status","warehouse_id");--> statement-breakpoint
CREATE INDEX "serial_units_serial_idx" ON "serial_units" USING btree ("tenant_id","serial_number");--> statement-breakpoint
CREATE UNIQUE INDEX "tasks_tenant_number_uq" ON "tasks" USING btree ("tenant_id","task_number");--> statement-breakpoint
CREATE INDEX "tasks_queue_idx" ON "tasks" USING btree ("tenant_id","warehouse_id","type","status","priority","sequence");--> statement-breakpoint
CREATE INDEX "tasks_assignee_idx" ON "tasks" USING btree ("tenant_id","assigned_to_id","status");--> statement-breakpoint
CREATE INDEX "tasks_group_idx" ON "tasks" USING btree ("tenant_id","group_key");--> statement-breakpoint
CREATE INDEX "tasks_ref_idx" ON "tasks" USING btree ("tenant_id","ref_type","ref_id");--> statement-breakpoint
CREATE INDEX "allocations_line_idx" ON "allocations" USING btree ("tenant_id","order_line_id");--> statement-breakpoint
CREATE INDEX "allocations_balance_status_idx" ON "allocations" USING btree ("tenant_id","balance_id","status");--> statement-breakpoint
CREATE INDEX "allocations_order_idx" ON "allocations" USING btree ("tenant_id","order_id");--> statement-breakpoint
CREATE INDEX "carton_lines_order_line_idx" ON "carton_lines" USING btree ("tenant_id","order_line_id");--> statement-breakpoint
CREATE UNIQUE INDEX "cartons_tenant_number_uq" ON "cartons" USING btree ("tenant_id","carton_number");--> statement-breakpoint
CREATE UNIQUE INDEX "loads_tenant_number_uq" ON "loads" USING btree ("tenant_id","load_number");--> statement-breakpoint
CREATE INDEX "loads_wh_status_idx" ON "loads" USING btree ("tenant_id","warehouse_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "order_lines_order_lineno_uq" ON "outbound_order_lines" USING btree ("order_id","line_no");--> statement-breakpoint
CREATE INDEX "order_lines_sku_idx" ON "outbound_order_lines" USING btree ("tenant_id","sku_id");--> statement-breakpoint
CREATE UNIQUE INDEX "orders_tenant_number_uq" ON "outbound_orders" USING btree ("tenant_id","order_number");--> statement-breakpoint
CREATE INDEX "orders_wh_status_prio_idx" ON "outbound_orders" USING btree ("tenant_id","warehouse_id","status","priority");--> statement-breakpoint
CREATE INDEX "orders_storer_ref_idx" ON "outbound_orders" USING btree ("tenant_id","storer_id","external_ref");--> statement-breakpoint
CREATE INDEX "orders_wave_idx" ON "outbound_orders" USING btree ("tenant_id","wave_id");--> statement-breakpoint
CREATE UNIQUE INDEX "return_order_lines_uq" ON "return_order_lines" USING btree ("return_id","line_no");--> statement-breakpoint
CREATE UNIQUE INDEX "return_orders_number_uq" ON "return_orders" USING btree ("tenant_id","rma_number");--> statement-breakpoint
CREATE INDEX "return_orders_wh_status_idx" ON "return_orders" USING btree ("tenant_id","warehouse_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "shipments_tenant_number_uq" ON "shipments" USING btree ("tenant_id","shipment_number");--> statement-breakpoint
CREATE INDEX "shipments_order_idx" ON "shipments" USING btree ("tenant_id","order_id");--> statement-breakpoint
CREATE INDEX "shipments_load_idx" ON "shipments" USING btree ("tenant_id","load_id");--> statement-breakpoint
CREATE UNIQUE INDEX "waves_tenant_number_uq" ON "waves" USING btree ("tenant_id","wave_number");--> statement-breakpoint
CREATE INDEX "waves_wh_status_idx" ON "waves" USING btree ("tenant_id","warehouse_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "billing_contracts_number_uq" ON "billing_contracts" USING btree ("tenant_id","contract_number");--> statement-breakpoint
CREATE INDEX "billing_contracts_storer_status_idx" ON "billing_contracts" USING btree ("tenant_id","storer_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "charge_events_idem_uq" ON "charge_events" USING btree ("tenant_id","idempotency_key");--> statement-breakpoint
CREATE INDEX "charge_events_storer_period_idx" ON "charge_events" USING btree ("tenant_id","storer_id","period_key","status");--> statement-breakpoint
CREATE UNIQUE INDEX "credit_notes_number_uq" ON "credit_notes" USING btree ("tenant_id","credit_number");--> statement-breakpoint
CREATE INDEX "invoice_lines_invoice_idx" ON "invoice_lines" USING btree ("tenant_id","invoice_id");--> statement-breakpoint
CREATE UNIQUE INDEX "invoices_tenant_number_uq" ON "invoices" USING btree ("tenant_id","invoice_number");--> statement-breakpoint
CREATE INDEX "invoices_storer_status_idx" ON "invoices" USING btree ("tenant_id","storer_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "storage_snapshots_uq" ON "storage_snapshots" USING btree ("tenant_id","warehouse_id","storer_id","snapshot_date");--> statement-breakpoint
CREATE INDEX "storage_snapshots_storer_date_idx" ON "storage_snapshots" USING btree ("tenant_id","storer_id","snapshot_date");--> statement-breakpoint
CREATE UNIQUE INDEX "tariffs_contract_code_uq" ON "tariffs" USING btree ("contract_id","code");--> statement-breakpoint
CREATE INDEX "tariffs_trigger_idx" ON "tariffs" USING btree ("tenant_id","trigger","active");--> statement-breakpoint
CREATE INDEX "import_jobs_status_idx" ON "import_jobs" USING btree ("tenant_id","status");--> statement-breakpoint
CREATE INDEX "integration_connectors_type_idx" ON "integration_connectors" USING btree ("tenant_id","type","active");--> statement-breakpoint
CREATE UNIQUE INDEX "integration_messages_idem_uq" ON "integration_messages" USING btree ("tenant_id","idempotency_key");--> statement-breakpoint
CREATE INDEX "integration_messages_status_idx" ON "integration_messages" USING btree ("tenant_id","status","created_at");--> statement-breakpoint
CREATE INDEX "integration_messages_type_ext_idx" ON "integration_messages" USING btree ("tenant_id","message_type","external_id");--> statement-breakpoint
CREATE INDEX "webhook_deliveries_status_next_idx" ON "webhook_deliveries" USING btree ("tenant_id","status","next_attempt_at");--> statement-breakpoint
CREATE INDEX "webhook_subscriptions_active_idx" ON "webhook_subscriptions" USING btree ("tenant_id","active");--> statement-breakpoint
CREATE INDEX "ai_conversations_user_idx" ON "ai_conversations" USING btree ("tenant_id","user_id");--> statement-breakpoint
CREATE INDEX "ai_messages_conv_idx" ON "ai_messages" USING btree ("conversation_id","created_at");--> statement-breakpoint
CREATE INDEX "ai_proposals_lookup_idx" ON "ai_proposals" USING btree ("tenant_id","type","status","created_at");--> statement-breakpoint
CREATE INDEX "attachments_ref_idx" ON "attachments" USING btree ("tenant_id","ref_type","ref_id");--> statement-breakpoint
CREATE INDEX "audit_logs_entity_idx" ON "audit_logs" USING btree ("tenant_id","entity_type","entity_id");--> statement-breakpoint
CREATE INDEX "audit_logs_time_idx" ON "audit_logs" USING btree ("tenant_id","created_at");--> statement-breakpoint
CREATE INDEX "documents_ref_idx" ON "documents" USING btree ("tenant_id","ref_type","ref_id");--> statement-breakpoint
CREATE INDEX "email_outbox_status_idx" ON "email_outbox" USING btree ("tenant_id","status");--> statement-breakpoint
CREATE INDEX "notifications_user_idx" ON "notifications" USING btree ("tenant_id","user_id","read_at");--> statement-breakpoint
CREATE INDEX "outbox_events_status_idx" ON "outbox_events" USING btree ("status","created_at");