CREATE TABLE "account" ( "id" text PRIMARY KEY NOT NULL, "account_id" text NOT NULL, "provider_id" text NOT NULL, "user_id" text NOT NULL, "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamp, "refresh_token_expires_at" timestamp, "scope" text, "password" text, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "agent_profile" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "enabled" boolean DEFAULT false NOT NULL, "name" text DEFAULT 'Asistente' NOT NULL, "tone" text, "instructions" text, "escalation_rules" text, "greeting" text, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "agent_test_case" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "run_id" text NOT NULL, "persona" text NOT NULL, "conversation_id" text, "transcript" jsonb, "veredicto" text, "hallazgos" jsonb, "status" text DEFAULT 'pending' NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "agent_test_run" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "status" text DEFAULT 'running' NOT NULL, "score" integer, "error" text, "started_at" timestamp DEFAULT now() NOT NULL, "finished_at" timestamp ); --> statement-breakpoint CREATE TABLE "contact" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "phone" text NOT NULL, "name" text NOT NULL, "notes" text, "archived_at" timestamp, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "conversation" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "contact_id" text NOT NULL, "is_test" boolean DEFAULT false NOT NULL, "ai_enabled" boolean DEFAULT true NOT NULL, "handoff_at" timestamp, "handoff_reason" text, "last_inbound_at" timestamp, "last_message_at" timestamp, "unread_count" integer DEFAULT 0 NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "invitation" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "email" text NOT NULL, "role" text, "status" text DEFAULT 'pending' NOT NULL, "expires_at" timestamp NOT NULL, "inviter_id" text NOT NULL ); --> statement-breakpoint CREATE TABLE "kb_entry" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "kind" text NOT NULL, "question" text, "answer" text, "content" text, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "lead" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "contact_id" text NOT NULL, "stage_id" text NOT NULL, "position" integer DEFAULT 0 NOT NULL, "last_activity_at" timestamp, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "member" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "user_id" text NOT NULL, "role" text DEFAULT 'member' NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "message" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "conversation_id" text NOT NULL, "wa_message_id" text, "direction" text NOT NULL, "type" text DEFAULT 'text' NOT NULL, "text" text, "status" text DEFAULT 'pending' NOT NULL, "error" text, "ai_generated" boolean DEFAULT false NOT NULL, "wa_timestamp" timestamp, "created_at" timestamp DEFAULT now() NOT NULL, CONSTRAINT "message_wa_message_id_unique" UNIQUE("wa_message_id") ); --> statement-breakpoint CREATE TABLE "meta_credentials" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "waba_id" text NOT NULL, "phone_number_id" text NOT NULL, "display_phone_number" text, "verified_name" text, "token_cipher" text NOT NULL, "token_iv" text NOT NULL, "token_tag" text NOT NULL, "status" text DEFAULT 'connected' NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "organization" ( "id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "slug" text, "logo" text, "created_at" timestamp DEFAULT now() NOT NULL, "metadata" text, CONSTRAINT "organization_slug_unique" UNIQUE("slug") ); --> statement-breakpoint CREATE TABLE "pipeline_stage" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "name" text NOT NULL, "position" integer NOT NULL, "kind" text DEFAULT 'open' NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "session" ( "id" text PRIMARY KEY NOT NULL, "expires_at" timestamp NOT NULL, "token" text NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL, "ip_address" text, "user_agent" text, "user_id" text NOT NULL, "active_organization_id" text, CONSTRAINT "session_token_unique" UNIQUE("token") ); --> statement-breakpoint CREATE TABLE "template" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "name" text NOT NULL, "language" text NOT NULL, "category" text NOT NULL, "body" text NOT NULL, "status" text DEFAULT 'draft' NOT NULL, "rejection_reason" text, "wa_template_id" text, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "user" ( "id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "email" text NOT NULL, "email_verified" boolean DEFAULT false NOT NULL, "image" text, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL, CONSTRAINT "user_email_unique" UNIQUE("email") ); --> statement-breakpoint CREATE TABLE "verification" ( "id" text PRIMARY KEY NOT NULL, "identifier" text NOT NULL, "value" text NOT NULL, "expires_at" timestamp NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_profile" ADD CONSTRAINT "agent_profile_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_test_case" ADD CONSTRAINT "agent_test_case_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_test_case" ADD CONSTRAINT "agent_test_case_run_id_agent_test_run_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."agent_test_run"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_test_case" ADD CONSTRAINT "agent_test_case_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_test_run" ADD CONSTRAINT "agent_test_run_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "contact" ADD CONSTRAINT "contact_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "conversation" ADD CONSTRAINT "conversation_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "conversation" ADD CONSTRAINT "conversation_contact_id_contact_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invitation" ADD CONSTRAINT "invitation_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invitation" ADD CONSTRAINT "invitation_inviter_id_user_id_fk" FOREIGN KEY ("inviter_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "kb_entry" ADD CONSTRAINT "kb_entry_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead" ADD CONSTRAINT "lead_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead" ADD CONSTRAINT "lead_contact_id_contact_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead" ADD CONSTRAINT "lead_stage_id_pipeline_stage_id_fk" FOREIGN KEY ("stage_id") REFERENCES "public"."pipeline_stage"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "member" ADD CONSTRAINT "member_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "member" ADD CONSTRAINT "member_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "message" ADD CONSTRAINT "message_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "message" ADD CONSTRAINT "message_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "meta_credentials" ADD CONSTRAINT "meta_credentials_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "pipeline_stage" ADD CONSTRAINT "pipeline_stage_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "template" ADD CONSTRAINT "template_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "agent_profile_org_uq" ON "agent_profile" USING btree ("organization_id");--> statement-breakpoint CREATE INDEX "test_case_run_idx" ON "agent_test_case" USING btree ("run_id");--> statement-breakpoint CREATE UNIQUE INDEX "test_run_org_running_uq" ON "agent_test_run" USING btree ("organization_id") WHERE "agent_test_run"."status" = 'running';--> statement-breakpoint CREATE INDEX "test_run_org_idx" ON "agent_test_run" USING btree ("organization_id","started_at");--> statement-breakpoint CREATE UNIQUE INDEX "contact_org_phone_uq" ON "contact" USING btree ("organization_id","phone");--> statement-breakpoint CREATE INDEX "contact_org_name_idx" ON "contact" USING btree ("organization_id","name");--> statement-breakpoint CREATE UNIQUE INDEX "conversation_org_contact_real_uq" ON "conversation" USING btree ("organization_id","contact_id") WHERE "conversation"."is_test" = false;--> statement-breakpoint CREATE INDEX "conversation_org_last_idx" ON "conversation" USING btree ("organization_id","last_message_at");--> statement-breakpoint CREATE INDEX "kb_org_idx" ON "kb_entry" USING btree ("organization_id");--> statement-breakpoint CREATE UNIQUE INDEX "lead_contact_uq" ON "lead" USING btree ("contact_id");--> statement-breakpoint CREATE INDEX "lead_org_stage_idx" ON "lead" USING btree ("organization_id","stage_id","position");--> statement-breakpoint CREATE INDEX "message_org_conv_idx" ON "message" USING btree ("organization_id","conversation_id","created_at");--> statement-breakpoint CREATE UNIQUE INDEX "meta_credentials_org_uq" ON "meta_credentials" USING btree ("organization_id");--> statement-breakpoint CREATE UNIQUE INDEX "meta_credentials_phone_uq" ON "meta_credentials" USING btree ("phone_number_id");--> statement-breakpoint CREATE INDEX "stage_org_pos_idx" ON "pipeline_stage" USING btree ("organization_id","position");--> statement-breakpoint CREATE UNIQUE INDEX "template_org_name_lang_uq" ON "template" USING btree ("organization_id","name","language");