-- CreateEnum
CREATE TYPE "IndustrialParkStatus" AS ENUM ('PLANNING', 'UNDER_CONSTRUCTION', 'OPERATIONAL', 'FULL');

-- CreateEnum
CREATE TYPE "IndustrialPropertyType" AS ENUM ('INDUSTRIAL_LAND', 'READY_BUILT_FACTORY', 'READY_BUILT_WAREHOUSE', 'LOGISTICS_CENTER', 'OFFICE_IN_PARK', 'DATA_CENTER');

-- CreateEnum
CREATE TYPE "IndustrialLeaseType" AS ENUM ('LAND_LEASE', 'FACTORY_LEASE', 'WAREHOUSE_LEASE', 'SUBLEASE');

-- CreateEnum
CREATE TYPE "IndustrialListingStatus" AS ENUM ('DRAFT', 'ACTIVE', 'RESERVED', 'LEASED', 'EXPIRED');

-- CreateEnum
CREATE TYPE "VietnamRegion" AS ENUM ('NORTH', 'CENTRAL', 'SOUTH');

-- CreateTable
CREATE TABLE "IndustrialPark" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "nameEn" TEXT,
    "slug" TEXT NOT NULL,
    "developer" TEXT NOT NULL,
    "operator" TEXT,
    "status" "IndustrialParkStatus" NOT NULL DEFAULT 'PLANNING',
    "location" geometry(Point, 4326) NOT NULL,
    "address" TEXT NOT NULL,
    "district" TEXT NOT NULL,
    "province" TEXT NOT NULL,
    "region" "VietnamRegion" NOT NULL,
    "totalAreaHa" DOUBLE PRECISION NOT NULL,
    "leasableAreaHa" DOUBLE PRECISION NOT NULL,
    "occupancyRate" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "remainingAreaHa" DOUBLE PRECISION NOT NULL,
    "tenantCount" INTEGER NOT NULL DEFAULT 0,
    "establishedYear" INTEGER,
    "landRentUsdM2Year" DOUBLE PRECISION,
    "rbfRentUsdM2Month" DOUBLE PRECISION,
    "rbwRentUsdM2Month" DOUBLE PRECISION,
    "managementFeeUsd" DOUBLE PRECISION,
    "infrastructure" JSONB,
    "connectivity" JSONB,
    "incentives" JSONB,
    "targetIndustries" TEXT[],
    "existingTenants" JSONB,
    "certifications" JSONB,
    "media" JSONB,
    "documents" JSONB,
    "description" TEXT,
    "descriptionEn" TEXT,
    "isVerified" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "IndustrialListing" (
    "id" TEXT NOT NULL,
    "parkId" TEXT NOT NULL,
    "agentId" TEXT,
    "sellerId" TEXT NOT NULL,
    "propertyType" "IndustrialPropertyType" NOT NULL,
    "leaseType" "IndustrialLeaseType" NOT NULL,
    "status" "IndustrialListingStatus" NOT NULL DEFAULT 'DRAFT',
    "title" TEXT NOT NULL,
    "description" TEXT,
    "areaM2" DOUBLE PRECISION NOT NULL,
    "ceilingHeightM" DOUBLE PRECISION,
    "floorLoadTonM2" DOUBLE PRECISION,
    "columnSpacingM" DOUBLE PRECISION,
    "dockCount" INTEGER,
    "craneCapacityTon" DOUBLE PRECISION,
    "hasMezzanine" BOOLEAN NOT NULL DEFAULT false,
    "hasOfficeArea" BOOLEAN NOT NULL DEFAULT false,
    "officeAreaM2" DOUBLE PRECISION,
    "priceUsdM2" DOUBLE PRECISION,
    "pricingUnit" TEXT,
    "totalLeasePrice" DOUBLE PRECISION,
    "managementFee" DOUBLE PRECISION,
    "depositMonths" INTEGER,
    "minLeaseYears" INTEGER,
    "maxLeaseYears" INTEGER,
    "leaseExpiry" TIMESTAMP(3),
    "availableFrom" TIMESTAMP(3),
    "powerCapacityKva" DOUBLE PRECISION,
    "waterSupplyM3Day" DOUBLE PRECISION,
    "media" JSONB,
    "viewCount" INTEGER NOT NULL DEFAULT 0,
    "inquiryCount" INTEGER NOT NULL DEFAULT 0,
    "publishedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "IndustrialPark_slug_key" ON "IndustrialPark"("slug");

-- CreateIndex
CREATE INDEX "IndustrialPark_status_idx" ON "IndustrialPark"("status");

-- CreateIndex
CREATE INDEX "IndustrialPark_province_idx" ON "IndustrialPark"("province");

-- CreateIndex
CREATE INDEX "IndustrialPark_region_idx" ON "IndustrialPark"("region");

-- CreateIndex
CREATE INDEX "IndustrialPark_developer_idx" ON "IndustrialPark"("developer");

-- CreateIndex
CREATE INDEX "IndustrialPark_location_idx" ON "IndustrialPark" USING GIST ("location");

-- CreateIndex
CREATE INDEX "IndustrialPark_isVerified_idx" ON "IndustrialPark"("isVerified");

-- CreateIndex
CREATE INDEX "IndustrialPark_occupancyRate_idx" ON "IndustrialPark"("occupancyRate");

-- CreateIndex
CREATE INDEX "IndustrialPark_landRentUsdM2Year_idx" ON "IndustrialPark"("landRentUsdM2Year");

-- CreateIndex
CREATE INDEX "IndustrialPark_region_province_status_idx" ON "IndustrialPark"("region", "province", "status");

-- CreateIndex
CREATE INDEX "IndustrialPark_createdAt_idx" ON "IndustrialPark"("createdAt");

-- CreateIndex
CREATE INDEX "IndustrialListing_parkId_idx" ON "IndustrialListing"("parkId");

-- CreateIndex
CREATE INDEX "IndustrialListing_propertyType_idx" ON "IndustrialListing"("propertyType");

-- CreateIndex
CREATE INDEX "IndustrialListing_leaseType_idx" ON "IndustrialListing"("leaseType");

-- CreateIndex
CREATE INDEX "IndustrialListing_status_idx" ON "IndustrialListing"("status");

-- CreateIndex
CREATE INDEX "IndustrialListing_areaM2_idx" ON "IndustrialListing"("areaM2");

-- CreateIndex
CREATE INDEX "IndustrialListing_priceUsdM2_idx" ON "IndustrialListing"("priceUsdM2");

-- CreateIndex
CREATE INDEX "IndustrialListing_sellerId_idx" ON "IndustrialListing"("sellerId");

-- CreateIndex
CREATE INDEX "IndustrialListing_agentId_idx" ON "IndustrialListing"("agentId");

-- CreateIndex
CREATE INDEX "IndustrialListing_publishedAt_idx" ON "IndustrialListing"("publishedAt");

-- CreateIndex
CREATE INDEX "IndustrialListing_parkId_status_idx" ON "IndustrialListing"("parkId", "status");

-- CreateIndex
CREATE INDEX "IndustrialListing_propertyType_leaseType_status_idx" ON "IndustrialListing"("propertyType", "leaseType", "status");

-- CreateIndex
CREATE INDEX "IndustrialListing_status_publishedAt_idx" ON "IndustrialListing"("status", "publishedAt" DESC);

-- AddForeignKey
ALTER TABLE "IndustrialListing" ADD CONSTRAINT "IndustrialListing_parkId_fkey" FOREIGN KEY ("parkId") REFERENCES "IndustrialPark"("id") ON DELETE CASCADE ON UPDATE CASCADE;
