CREATE TYPE "public"."freshness_state" AS ENUM('fresh', 'watch', 'at_risk', 'expired');--> statement-breakpoint CREATE TYPE "public"."integration_provider" AS ENUM('odoo', 'salesforce');--> statement-breakpoint CREATE TYPE "public"."member_role" AS ENUM('owner', 'manager', 'viewer');--> statement-breakpoint CREATE TYPE "public"."recommendation_state" AS ENUM('pending', 'reviewed', 'dismissed');--> statement-breakpoint CREATE TYPE "public"."sync_state" AS ENUM('queued', 'running', 'succeeded', 'failed');--> statement-breakpoint CREATE TABLE "alerts" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "inventory_batch_id" uuid, "severity" text NOT NULL, "title" text NOT NULL, "message" text NOT NULL, "acknowledged_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 "audit_events" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "user_id" uuid, "action" text NOT NULL, "entity_type" text NOT NULL, "entity_id" text NOT NULL, "metadata" jsonb DEFAULT '{}'::jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "branches" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "name" text NOT NULL, "city" text NOT NULL, "region" text NOT NULL, "source_id" 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 "integration_connections" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "provider" "integration_provider" NOT NULL, "display_name" text NOT NULL, "enabled" boolean DEFAULT false NOT NULL, "encrypted_credentials" text, "configuration" jsonb DEFAULT '{}'::jsonb NOT NULL, "last_synced_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_batches" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "product_id" uuid NOT NULL, "branch_id" uuid NOT NULL, "received_at" timestamp with time zone NOT NULL, "quantity_received" integer NOT NULL, "quantity_on_hand" integer NOT NULL, "freshness_score" integer NOT NULL, "freshness_state" "freshness_state" NOT NULL, "source_id" text 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 "memberships" ( "organization_id" uuid NOT NULL, "user_id" uuid NOT NULL, "role" "member_role" DEFAULT 'viewer' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "memberships_organization_id_user_id_pk" PRIMARY KEY("organization_id","user_id") ); --> statement-breakpoint CREATE TABLE "organizations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "slug" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "organizations_slug_unique" UNIQUE("slug") ); --> statement-breakpoint CREATE TABLE "products" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "sku" text NOT NULL, "name" text NOT NULL, "category" text NOT NULL, "unit" text NOT NULL, "shelf_life_days" integer NOT NULL, "cost" numeric(12, 2) NOT NULL, "list_price" numeric(12, 2) NOT NULL, "margin_floor_percent" numeric(5, 2) NOT NULL, "source_system" "integration_provider" NOT NULL, "source_id" text 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 "recommendations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "product_id" uuid NOT NULL, "branch_id" uuid NOT NULL, "type" text NOT NULL, "current_price" numeric(12, 2) NOT NULL, "recommended_low" numeric(12, 2) NOT NULL, "recommended_high" numeric(12, 2) NOT NULL, "confidence" integer NOT NULL, "explanation" text NOT NULL, "state" "recommendation_state" DEFAULT 'pending' NOT NULL, "expires_at" timestamp with time zone NOT NULL, "reviewed_by" 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 "sales" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "product_id" uuid NOT NULL, "branch_id" uuid NOT NULL, "salesperson_id" uuid, "sold_at" timestamp with time zone NOT NULL, "quantity" integer NOT NULL, "unit_price" numeric(12, 2) NOT NULL, "source_id" text 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 "salespeople" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "branch_id" uuid NOT NULL, "name" text NOT NULL, "email" text, "source_id" 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 "sync_runs" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "connection_id" uuid, "provider" "integration_provider" NOT NULL, "state" "sync_state" DEFAULT 'queued' NOT NULL, "cursor" text, "records_read" integer DEFAULT 0 NOT NULL, "records_written" integer DEFAULT 0 NOT NULL, "error" text, "started_at" timestamp with time zone, "finished_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "users" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "email" text NOT NULL, "name" text NOT NULL, "password_hash" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "users_email_unique" UNIQUE("email") ); --> statement-breakpoint ALTER TABLE "alerts" ADD CONSTRAINT "alerts_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "alerts" ADD CONSTRAINT "alerts_inventory_batch_id_inventory_batches_id_fk" FOREIGN KEY ("inventory_batch_id") REFERENCES "public"."inventory_batches"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "audit_events" ADD CONSTRAINT "audit_events_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "audit_events" ADD CONSTRAINT "audit_events_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "branches" ADD CONSTRAINT "branches_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "integration_connections" ADD CONSTRAINT "integration_connections_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "inventory_batches" ADD CONSTRAINT "inventory_batches_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "inventory_batches" ADD CONSTRAINT "inventory_batches_product_id_products_id_fk" FOREIGN KEY ("product_id") REFERENCES "public"."products"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "inventory_batches" ADD CONSTRAINT "inventory_batches_branch_id_branches_id_fk" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "memberships" ADD CONSTRAINT "memberships_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "memberships" ADD CONSTRAINT "memberships_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "products" ADD CONSTRAINT "products_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "recommendations" ADD CONSTRAINT "recommendations_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "recommendations" ADD CONSTRAINT "recommendations_product_id_products_id_fk" FOREIGN KEY ("product_id") REFERENCES "public"."products"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "recommendations" ADD CONSTRAINT "recommendations_branch_id_branches_id_fk" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "recommendations" ADD CONSTRAINT "recommendations_reviewed_by_users_id_fk" FOREIGN KEY ("reviewed_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sales" ADD CONSTRAINT "sales_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sales" ADD CONSTRAINT "sales_product_id_products_id_fk" FOREIGN KEY ("product_id") REFERENCES "public"."products"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sales" ADD CONSTRAINT "sales_branch_id_branches_id_fk" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sales" ADD CONSTRAINT "sales_salesperson_id_salespeople_id_fk" FOREIGN KEY ("salesperson_id") REFERENCES "public"."salespeople"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "salespeople" ADD CONSTRAINT "salespeople_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "salespeople" ADD CONSTRAINT "salespeople_branch_id_branches_id_fk" FOREIGN KEY ("branch_id") REFERENCES "public"."branches"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sync_runs" ADD CONSTRAINT "sync_runs_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sync_runs" ADD CONSTRAINT "sync_runs_connection_id_integration_connections_id_fk" FOREIGN KEY ("connection_id") REFERENCES "public"."integration_connections"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE INDEX "alerts_tenant_idx" ON "alerts" USING btree ("organization_id");--> statement-breakpoint CREATE INDEX "branches_organization_idx" ON "branches" USING btree ("organization_id");--> statement-breakpoint CREATE UNIQUE INDEX "branches_source_unique" ON "branches" USING btree ("organization_id","source_id");--> statement-breakpoint CREATE UNIQUE INDEX "integration_provider_unique" ON "integration_connections" USING btree ("organization_id","provider");--> statement-breakpoint CREATE INDEX "inventory_tenant_product_idx" ON "inventory_batches" USING btree ("organization_id","product_id");--> statement-breakpoint CREATE UNIQUE INDEX "inventory_source_unique" ON "inventory_batches" USING btree ("organization_id","source_id");--> statement-breakpoint CREATE UNIQUE INDEX "products_sku_unique" ON "products" USING btree ("organization_id","sku");--> statement-breakpoint CREATE UNIQUE INDEX "products_source_unique" ON "products" USING btree ("organization_id","source_system","source_id");--> statement-breakpoint CREATE INDEX "sales_tenant_date_idx" ON "sales" USING btree ("organization_id","sold_at");--> statement-breakpoint CREATE UNIQUE INDEX "sales_source_unique" ON "sales" USING btree ("organization_id","source_id");--> statement-breakpoint CREATE INDEX "salespeople_organization_idx" ON "salespeople" USING btree ("organization_id");