CREATE TABLE "campaign" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "area_id" uuid NOT NULL, "name" text NOT NULL, "status" text DEFAULT 'active' NOT NULL, "reach" integer DEFAULT 0 NOT NULL, "owner_user_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "deal" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "area_id" uuid NOT NULL, "name" text NOT NULL, "owner_user_id" uuid, "value_eur" integer DEFAULT 0 NOT NULL, "probability" integer DEFAULT 50 NOT NULL, "stage" text NOT NULL, "entered_stage_at" timestamp with time zone DEFAULT now() NOT NULL, "won_at" timestamp with time zone, "won_value_eur" integer, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "gtm_step" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "product" text NOT NULL, "title" text NOT NULL, "owner_user_id" uuid, "status" text DEFAULT 'open' NOT NULL, "order" integer DEFAULT 0 NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "lead_handover" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "campaign_id" uuid, "deal_id" uuid, "uebergeben_am" timestamp with time zone DEFAULT now() NOT NULL, "uebergeben_von" uuid ); --> statement-breakpoint ALTER TABLE "campaign" ADD CONSTRAINT "campaign_area_id_area_id_fk" FOREIGN KEY ("area_id") REFERENCES "public"."area"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "campaign" ADD CONSTRAINT "campaign_owner_user_id_user_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "deal" ADD CONSTRAINT "deal_area_id_area_id_fk" FOREIGN KEY ("area_id") REFERENCES "public"."area"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "deal" ADD CONSTRAINT "deal_owner_user_id_user_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "gtm_step" ADD CONSTRAINT "gtm_step_owner_user_id_user_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_handover" ADD CONSTRAINT "lead_handover_campaign_id_campaign_id_fk" FOREIGN KEY ("campaign_id") REFERENCES "public"."campaign"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_handover" ADD CONSTRAINT "lead_handover_deal_id_deal_id_fk" FOREIGN KEY ("deal_id") REFERENCES "public"."deal"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_handover" ADD CONSTRAINT "lead_handover_uebergeben_von_user_id_fk" FOREIGN KEY ("uebergeben_von") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint CREATE INDEX "idx_campaign_status" ON "campaign" USING btree ("status");--> statement-breakpoint CREATE INDEX "idx_deal_stage" ON "deal" USING btree ("stage");--> statement-breakpoint CREATE INDEX "idx_deal_owner" ON "deal" USING btree ("owner_user_id");--> statement-breakpoint CREATE INDEX "idx_gtm_product" ON "gtm_step" USING btree ("product");