CREATE TABLE "estimates" ("id" uuid NOT NULL PRIMARY KEY, "created_at" timestamp with time zone NOT NULL, "updated_at" timestamp with time zone NOT NULL, "deleted_at" timestamp with time zone NULL, "is_active" boolean NOT NULL, "remarks" text NOT NULL, "estimate_number" varchar(50) NOT NULL, "customer_name" varchar(200) NOT NULL, "customer_email" varchar(254) NOT NULL, "customer_phone" varchar(20) NOT NULL, "company_name" varchar(200) NOT NULL, "subtotal" numeric(16, 2) NOT NULL, "tax" numeric(16, 2) NOT NULL, "total" numeric(16, 2) NOT NULL, "currency" varchar(3) NOT NULL, "status" varchar(15) NOT NULL, "approved_at" timestamp with time zone NULL, "rejection_reason" text NOT NULL, "approved_by_id" uuid NULL, "created_by_id" uuid NULL, "deleted_by_id" uuid NULL, "organization_id" uuid NOT NULL, "project_id" uuid NULL, "updated_by_id" uuid NULL);
ALTER TABLE "estimates" ADD COLUMN "proposal_id" uuid NULL CONSTRAINT "estimates_proposal_id_c4d37108_fk_proposals_id" REFERENCES "proposals"("id") DEFERRABLE INITIALLY DEFERRED; SET CONSTRAINTS "estimates_proposal_id_c4d37108_fk_proposals_id" IMMEDIATE;
ALTER TABLE "estimates" ADD CONSTRAINT "estimates_approved_by_id_445da18f_fk_users_id" FOREIGN KEY ("approved_by_id") REFERENCES "users" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "estimates" ADD CONSTRAINT "estimates_created_by_id_7e401493_fk_users_id" FOREIGN KEY ("created_by_id") REFERENCES "users" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "estimates" ADD CONSTRAINT "estimates_deleted_by_id_45b61be5_fk_users_id" FOREIGN KEY ("deleted_by_id") REFERENCES "users" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "estimates" ADD CONSTRAINT "estimates_organization_id_3a5cbf15_fk_organizations_id" FOREIGN KEY ("organization_id") REFERENCES "organizations" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "estimates" ADD CONSTRAINT "estimates_project_id_7f195a41_fk_projects_project_id" FOREIGN KEY ("project_id") REFERENCES "projects_project" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "estimates" ADD CONSTRAINT "estimates_updated_by_id_b3fcfb1d_fk_users_id" FOREIGN KEY ("updated_by_id") REFERENCES "users" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "estimates_created_at_7086459e" ON "estimates" ("created_at");
CREATE INDEX "estimates_deleted_at_93f00fb8" ON "estimates" ("deleted_at");
CREATE INDEX "estimates_is_active_7b98190e" ON "estimates" ("is_active");
CREATE INDEX "estimates_approved_by_id_445da18f" ON "estimates" ("approved_by_id");
CREATE INDEX "estimates_created_by_id_7e401493" ON "estimates" ("created_by_id");
CREATE INDEX "estimates_deleted_by_id_45b61be5" ON "estimates" ("deleted_by_id");
CREATE INDEX "estimates_organization_id_3a5cbf15" ON "estimates" ("organization_id");
CREATE INDEX "estimates_project_id_7f195a41" ON "estimates" ("project_id");
CREATE INDEX "estimates_updated_by_id_b3fcfb1d" ON "estimates" ("updated_by_id");
ALTER TABLE "estimate_items" ADD CONSTRAINT "estimate_items_estimate_id_fea336f7_fk_estimates_id" FOREIGN KEY ("estimate_id") REFERENCES "estimates" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_estimate_id_1e763a02_fk_estimates_id" FOREIGN KEY ("estimate_id") REFERENCES "estimates" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "estimates_proposal_id_c4d37108" ON "estimates" ("proposal_id");
