CREATE TABLE "billing_plans" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" text NOT NULL,
	"tier" text NOT NULL,
	"price_monthly" integer DEFAULT 0 NOT NULL,
	"price_annual" integer DEFAULT 0 NOT NULL,
	"features" text[] DEFAULT '{}'::text[] NOT NULL,
	"max_users" integer,
	"max_projects" integer,
	"archived_at" timestamp with time zone,
	"created_by" 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 "catalog_items" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"name" text NOT NULL,
	"description" text,
	"sku" text,
	"unit_price" numeric(12, 2) NOT NULL,
	"currency" text DEFAULT 'ILS' NOT NULL,
	"unit" text DEFAULT 'unit',
	"category" text,
	"is_active" boolean DEFAULT true NOT NULL,
	"metadata" jsonb,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "campaigns" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"name" text NOT NULL,
	"type" text DEFAULT 'email' NOT NULL,
	"status" text DEFAULT 'draft' NOT NULL,
	"target_segment" jsonb,
	"scheduled_at" timestamp with time zone,
	"sent_count" integer DEFAULT 0 NOT NULL,
	"is_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 "campaigns_type_check" CHECK ("campaigns"."type" IN ('email','sms','push')),
	CONSTRAINT "campaigns_status_check" CHECK ("campaigns"."status" IN ('draft','active','completed'))
);
--> statement-breakpoint
CREATE TABLE "proposals" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"customer_id" uuid,
	"lead_id" uuid,
	"title" text NOT NULL,
	"status" text DEFAULT 'draft' NOT NULL,
	"line_items" jsonb NOT NULL,
	"subtotal" numeric(12, 2),
	"discount" numeric(12, 2),
	"total" numeric(12, 2),
	"notes" text,
	"valid_until" timestamp with time zone,
	"sent_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 "proposals_status_check" CHECK ("proposals"."status" IN ('draft','sent','accepted','rejected'))
);
--> statement-breakpoint
CREATE INDEX "billing_plans_tier_idx" ON "billing_plans" USING btree ("tier");--> statement-breakpoint
CREATE INDEX "billing_plans_active_idx" ON "billing_plans" USING btree ("archived_at");--> statement-breakpoint
CREATE INDEX "idx_catalog_items_tenant" ON "catalog_items" USING btree ("tenant_id");--> statement-breakpoint
CREATE INDEX "idx_catalog_items_tenant_category" ON "catalog_items" USING btree ("tenant_id","category");--> statement-breakpoint
CREATE INDEX "idx_campaigns_tenant" ON "campaigns" USING btree ("tenant_id");--> statement-breakpoint
CREATE INDEX "idx_campaigns_tenant_status" ON "campaigns" USING btree ("tenant_id","status");--> statement-breakpoint
CREATE INDEX "idx_proposals_tenant" ON "proposals" USING btree ("tenant_id");--> statement-breakpoint
CREATE INDEX "idx_proposals_tenant_status" ON "proposals" USING btree ("tenant_id","status");--> statement-breakpoint
CREATE INDEX "idx_proposals_tenant_customer" ON "proposals" USING btree ("tenant_id","customer_id");