-- CreateEnum
CREATE TYPE "ProjectDevelopmentStatus" AS ENUM ('PLANNING', 'UNDER_CONSTRUCTION', 'COMPLETED', 'HANDOVER');

-- CreateTable
CREATE TABLE "ProjectDevelopment" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "developer" TEXT NOT NULL,
    "developerLogo" TEXT,
    "totalUnits" INTEGER NOT NULL,
    "completedUnits" INTEGER NOT NULL DEFAULT 0,
    "status" "ProjectDevelopmentStatus" NOT NULL DEFAULT 'PLANNING',
    "startDate" TIMESTAMP(3),
    "completionDate" TIMESTAMP(3),
    "description" TEXT,
    "amenities" JSONB,
    "masterPlanUrl" TEXT,
    "location" geometry(Point, 4326) NOT NULL,
    "address" TEXT NOT NULL,
    "ward" TEXT NOT NULL,
    "district" TEXT NOT NULL,
    "city" TEXT NOT NULL,
    "minPrice" BIGINT,
    "maxPrice" BIGINT,
    "pricePerM2Range" JSONB,
    "totalArea" DOUBLE PRECISION,
    "buildingCount" INTEGER,
    "floorCount" INTEGER,
    "unitTypes" JSONB,
    "media" JSONB,
    "documents" JSONB,
    "tags" TEXT[],
    "isVerified" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "ProjectDevelopment_pkey" PRIMARY KEY ("id")
);

-- CreateIndex: unique slug
CREATE UNIQUE INDEX "ProjectDevelopment_slug_key" ON "ProjectDevelopment"("slug");

-- CreateIndex: status filter
CREATE INDEX "ProjectDevelopment_status_idx" ON "ProjectDevelopment"("status");

-- CreateIndex: district + city lookup
CREATE INDEX "ProjectDevelopment_district_city_idx" ON "ProjectDevelopment"("district", "city");

-- CreateIndex: developer lookup
CREATE INDEX "ProjectDevelopment_developer_idx" ON "ProjectDevelopment"("developer");

-- CreateIndex: PostGIS spatial index
CREATE INDEX "ProjectDevelopment_location_idx" ON "ProjectDevelopment" USING GIST ("location");

-- CreateIndex: verified filter
CREATE INDEX "ProjectDevelopment_isVerified_idx" ON "ProjectDevelopment"("isVerified");

-- CreateIndex: created_at sorting
CREATE INDEX "ProjectDevelopment_createdAt_idx" ON "ProjectDevelopment"("createdAt");

-- CreateIndex: compound district + city + status
CREATE INDEX "ProjectDevelopment_district_city_status_idx" ON "ProjectDevelopment"("district", "city", "status");

-- AddColumn: FK from Property to ProjectDevelopment
ALTER TABLE "Property" ADD COLUMN "projectDevelopmentId" TEXT;

-- CreateIndex: Property.projectDevelopmentId
CREATE INDEX "Property_projectDevelopmentId_idx" ON "Property"("projectDevelopmentId");

-- AddForeignKey
ALTER TABLE "Property" ADD CONSTRAINT "Property_projectDevelopmentId_fkey" FOREIGN KEY ("projectDevelopmentId") REFERENCES "ProjectDevelopment"("id") ON DELETE SET NULL ON UPDATE CASCADE;
