CREATE TABLE "lead" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "area_id" uuid NOT NULL, "firma" text NOT NULL, "status" text DEFAULT 'cold' NOT NULL, "score" integer DEFAULT 0 NOT NULL, "owner_user_id" uuid, "branche" text, "sub_segment" text, "standorte" text, "mitarbeiterzahl" text, "umsatz" text, "website" text, "quelle" text, "pain_point" text, "status_quo_kosten" text, "bestehende_loesung" text, "wechselgrund" text, "persoenlicher_erfolg" text, "budget_indikation" text, "entscheidungshorizont" text, "compelling_event" text, "mitbewerber" text, "buying_center" text, "beschaffungsweg" text, "kommunikationsstil" text, "vorlieben" text, "hot_buttons" text, "no_gos" text, "druckpunkte" text, "anker" text, "vertrauenslevel" integer, "nutzen_hypothese" text, "differenzierer" text, "naechster_schritt" text, "naechster_schritt_datum" date, "risiken" text, "plan_b" text, "offene_fragen" jsonb DEFAULT '[]'::jsonb NOT NULL, "anhaenge" jsonb DEFAULT '[]'::jsonb NOT NULL, "promoted_deal_id" uuid, "created_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 "lead_contact" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "lead_id" uuid NOT NULL, "name" text NOT NULL, "rolle" text, "funktion" text, "email" text, "telefon" text, "linkedin" text, "im_buying_center_seit" text, "sprache" text, "is_primary" boolean DEFAULT false NOT NULL, "order" integer DEFAULT 0 NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "lead_touchpoint" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "lead_id" uuid NOT NULL, "datum" date NOT NULL, "kanal" text NOT NULL, "bearbeiter_user_id" uuid, "inhalt" text NOT NULL, "outcome" text, "follow_up_bis" date, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "lead" ADD CONSTRAINT "lead_area_id_area_id_fk" FOREIGN KEY ("area_id") REFERENCES "public"."area"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead" ADD CONSTRAINT "lead_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" ADD CONSTRAINT "lead_promoted_deal_id_deal_id_fk" FOREIGN KEY ("promoted_deal_id") REFERENCES "public"."deal"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead" ADD CONSTRAINT "lead_created_by_user_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_contact" ADD CONSTRAINT "lead_contact_lead_id_lead_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."lead"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_touchpoint" ADD CONSTRAINT "lead_touchpoint_lead_id_lead_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."lead"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_touchpoint" ADD CONSTRAINT "lead_touchpoint_bearbeiter_user_id_user_id_fk" FOREIGN KEY ("bearbeiter_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint CREATE INDEX "idx_lead_status" ON "lead" USING btree ("status");--> statement-breakpoint CREATE INDEX "idx_lead_owner" ON "lead" USING btree ("owner_user_id");--> statement-breakpoint CREATE INDEX "idx_lead_contact_lead" ON "lead_contact" USING btree ("lead_id");--> statement-breakpoint CREATE INDEX "idx_lead_touchpoint_lead" ON "lead_touchpoint" USING btree ("lead_id");--> statement-breakpoint CREATE INDEX "idx_lead_touchpoint_followup" ON "lead_touchpoint" USING btree ("follow_up_bis");