CREATE TYPE "public"."lead_estado" AS ENUM('nuevo', 'contactado', 'visita_agendada', 'presupuesto_enviado', 'ganado', 'perdido');--> statement-breakpoint CREATE TYPE "public"."pipeline_stage" AS ENUM('form_completado', 'fotos_subidas', 'prellamada_enviada', 'llamada_completada', 'render_generado', 'presupuesto_generado', 'whatsapp_entregado');--> statement-breakpoint CREATE TYPE "public"."tipo_reforma" AS ENUM('cocina', 'bano', 'salon', 'comedor', 'integral', 'otro');--> statement-breakpoint CREATE TABLE "lead_estado_history" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "lead_id" uuid NOT NULL, "estado" "lead_estado" NOT NULL, "changed_at" timestamp with time zone DEFAULT now() NOT NULL, "changed_by" text ); --> statement-breakpoint CREATE TABLE "lead_fotos" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "lead_id" uuid NOT NULL, "url" text NOT NULL, "orden" integer DEFAULT 0 NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "lead_pipeline_eventos" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "lead_id" uuid NOT NULL, "stage" "pipeline_stage" NOT NULL, "occurred_at" timestamp with time zone DEFAULT now() NOT NULL, "metadata" jsonb ); --> statement-breakpoint CREATE TABLE "leads" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "tenant_id" uuid NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "nombre" text NOT NULL, "telefono" text NOT NULL, "email" text NOT NULL, "provincia" text, "tipo_reforma" "tipo_reforma", "consent_privacidad" boolean DEFAULT false NOT NULL, "consent_contratacion" boolean DEFAULT false NOT NULL, "pipeline_stage" "pipeline_stage" DEFAULT 'form_completado' NOT NULL, "estado" "lead_estado" DEFAULT 'nuevo' NOT NULL, "presupuesto_estimado" integer, "transcripcion" text, "entidades" jsonb, "render_url" text, "pdf_url" text, "audio_url" text, "notas" text ); --> statement-breakpoint CREATE TABLE "precision_history" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "lead_id" uuid NOT NULL, "estimated" integer NOT NULL, "final" integer NOT NULL, "delta_pct" numeric(6, 2) NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "tenants" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "slug" text NOT NULL, "nombre_empresa" text NOT NULL, "logo_url" text, "provincia" text, "whatsapp_business" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "tenants_slug_unique" UNIQUE("slug") ); --> statement-breakpoint ALTER TABLE "lead_estado_history" ADD CONSTRAINT "lead_estado_history_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_fotos" ADD CONSTRAINT "lead_fotos_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_pipeline_eventos" ADD CONSTRAINT "lead_pipeline_eventos_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "leads" ADD CONSTRAINT "leads_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "precision_history" ADD CONSTRAINT "precision_history_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint CREATE INDEX "leads_tenant_created_idx" ON "leads" USING btree ("tenant_id","created_at");--> statement-breakpoint CREATE INDEX "leads_estado_idx" ON "leads" USING btree ("estado");