CREATE TABLE "enquiries" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "client_id" uuid NOT NULL, "pro_id" uuid NOT NULL, "responded_at" timestamp with time zone, "last_message_at" timestamp with time zone, "closed_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "enquiries_pair_unique" UNIQUE("client_id","pro_id") ); --> statement-breakpoint CREATE TABLE "pro_watches" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "watcher_id" uuid NOT NULL, "pro_id" uuid NOT NULL, "available_at_watch" text DEFAULT 'true' NOT NULL, "notified_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "pro_watches_unique" UNIQUE("watcher_id","pro_id") ); --> statement-breakpoint ALTER TABLE "messages" ALTER COLUMN "match_id" DROP NOT NULL;--> statement-breakpoint ALTER TABLE "messages" ADD COLUMN "enquiry_id" uuid;--> statement-breakpoint ALTER TABLE "enquiries" ADD CONSTRAINT "enquiries_client_id_users_id_fk" FOREIGN KEY ("client_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "enquiries" ADD CONSTRAINT "enquiries_pro_id_pro_profiles_user_id_fk" FOREIGN KEY ("pro_id") REFERENCES "public"."pro_profiles"("user_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "pro_watches" ADD CONSTRAINT "pro_watches_watcher_id_users_id_fk" FOREIGN KEY ("watcher_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "pro_watches" ADD CONSTRAINT "pro_watches_pro_id_pro_profiles_user_id_fk" FOREIGN KEY ("pro_id") REFERENCES "public"."pro_profiles"("user_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint CREATE INDEX "enquiries_pro_idx" ON "enquiries" USING btree ("pro_id","last_message_at");--> statement-breakpoint CREATE INDEX "enquiries_client_idx" ON "enquiries" USING btree ("client_id","last_message_at");--> statement-breakpoint CREATE INDEX "pro_watches_pro_idx" ON "pro_watches" USING btree ("pro_id","notified_at");--> statement-breakpoint CREATE INDEX "pro_watches_watcher_idx" ON "pro_watches" USING btree ("watcher_id","created_at");--> statement-breakpoint ALTER TABLE "messages" ADD CONSTRAINT "messages_enquiry_id_enquiries_id_fk" FOREIGN KEY ("enquiry_id") REFERENCES "public"."enquiries"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint CREATE INDEX "messages_enquiry_idx" ON "messages" USING btree ("enquiry_id","created_at");--> statement-breakpoint CREATE INDEX "messages_enquiry_unread_idx" ON "messages" USING btree ("enquiry_id","sender_id") WHERE "messages"."read_at" IS NULL;--> statement-breakpoint ALTER TABLE "messages" ADD CONSTRAINT "messages_one_parent" CHECK (("messages"."match_id" IS NULL) <> ("messages"."enquiry_id" IS NULL));