CREATE TYPE "public"."user_defined_deal_measure_source" AS ENUM('instructed', 'pibi_ordered');--> statement-breakpoint
CREATE TABLE "magic_plan_door" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"magic_plan_room_id" bigint NOT NULL,
	"width_mm" real,
	"type" text
);
--> statement-breakpoint
CREATE TABLE "magic_plan_floor" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"magic_plan_plan_id" bigint NOT NULL,
	"level" integer
);
--> statement-breakpoint
CREATE TABLE "magic_plan_plan" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"name" text,
	"address" text,
	"postcode" text
);
--> statement-breakpoint
CREATE TABLE "magic_plan_room" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"magic_plan_floor_id" bigint NOT NULL,
	"name" text,
	"width_m" real,
	"length_m" real,
	"area_m2" real
);
--> statement-breakpoint
CREATE TABLE "magic_plan_window" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"magic_plan_room_id" bigint NOT NULL,
	"width_m" real,
	"height_m" real,
	"area_m2" real,
	"opening_type" text
);
--> statement-breakpoint
CREATE TABLE "survey_requests" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"hubspot_deal_id" text NOT NULL,
	"portfolio_id" bigint NOT NULL,
	"notes" text NOT NULL,
	"status" text DEFAULT 'pending' NOT NULL,
	"requested_by" bigint NOT NULL,
	"requested_at" timestamp with time zone DEFAULT now() NOT NULL,
	"fulfilled_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "user_defined_deal_measures" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"hubspot_deal_id" text NOT NULL,
	"measure_name" text NOT NULL,
	"source" "user_defined_deal_measure_source" NOT NULL,
	"created_by_user_id" bigint NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"pushed_at" timestamp with time zone,
	"confirmed_in_hubspot_at" timestamp with time zone,
	"notes" text
);
--> statement-breakpoint
ALTER TABLE "hubspot_deal_data" ADD COLUMN "domna_survey_required" boolean;--> statement-breakpoint
ALTER TABLE "magic_plan_door" ADD CONSTRAINT "magic_plan_door_magic_plan_room_id_magic_plan_room_id_fk" FOREIGN KEY ("magic_plan_room_id") REFERENCES "public"."magic_plan_room"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "magic_plan_floor" ADD CONSTRAINT "magic_plan_floor_magic_plan_plan_id_magic_plan_plan_id_fk" FOREIGN KEY ("magic_plan_plan_id") REFERENCES "public"."magic_plan_plan"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "magic_plan_room" ADD CONSTRAINT "magic_plan_room_magic_plan_floor_id_magic_plan_floor_id_fk" FOREIGN KEY ("magic_plan_floor_id") REFERENCES "public"."magic_plan_floor"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "magic_plan_window" ADD CONSTRAINT "magic_plan_window_magic_plan_room_id_magic_plan_room_id_fk" FOREIGN KEY ("magic_plan_room_id") REFERENCES "public"."magic_plan_room"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "survey_requests" ADD CONSTRAINT "survey_requests_portfolio_id_portfolio_id_fk" FOREIGN KEY ("portfolio_id") REFERENCES "public"."portfolio"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "survey_requests" ADD CONSTRAINT "survey_requests_requested_by_user_id_fk" FOREIGN KEY ("requested_by") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_defined_deal_measures" ADD CONSTRAINT "user_defined_deal_measures_created_by_user_id_user_id_fk" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_survey_requests_deal_id" ON "survey_requests" USING btree ("hubspot_deal_id");--> statement-breakpoint
CREATE INDEX "idx_survey_requests_portfolio_id" ON "survey_requests" USING btree ("portfolio_id");--> statement-breakpoint
CREATE INDEX "idx_user_defined_deal_measures_deal_id" ON "user_defined_deal_measures" USING btree ("hubspot_deal_id");--> statement-breakpoint
CREATE INDEX "idx_user_defined_deal_measures_source" ON "user_defined_deal_measures" USING btree ("source");