-- CreateEnum
CREATE TYPE "ReportType" AS ENUM ('RESIDENTIAL_MARKET', 'INDUSTRIAL_MARKET', 'DISTRICT_ANALYSIS', 'INVESTMENT_FEASIBILITY', 'INDUSTRIAL_LOCATION', 'PROPERTY_VALUATION', 'PORTFOLIO');

-- CreateEnum
CREATE TYPE "ReportStatus" AS ENUM ('GENERATING', 'READY', 'FAILED');

-- AlterTable: Add maxReports to Plan
ALTER TABLE "Plan" ADD COLUMN "maxReports" INTEGER;

-- CreateTable: Report
CREATE TABLE "Report" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "type" "ReportType" NOT NULL,
    "title" TEXT NOT NULL,
    "params" JSONB NOT NULL,
    "content" JSONB,
    "pdfUrl" TEXT,
    "status" "ReportStatus" NOT NULL DEFAULT 'GENERATING',
    "errorMsg" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: MacroeconomicData
CREATE TABLE "MacroeconomicData" (
    "id" TEXT NOT NULL,
    "province" TEXT NOT NULL,
    "indicator" TEXT NOT NULL,
    "value" DOUBLE PRECISION NOT NULL,
    "unit" TEXT NOT NULL,
    "period" TEXT NOT NULL,
    "source" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable: InfrastructureProject
CREATE TABLE "InfrastructureProject" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "province" TEXT NOT NULL,
    "category" TEXT NOT NULL,
    "status" TEXT NOT NULL,
    "investmentVND" BIGINT,
    "startDate" TIMESTAMP(3),
    "completionDate" TIMESTAMP(3),
    "description" TEXT,
    "impactRadius" DOUBLE PRECISION,
    "location" geometry(Point, 4326),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "Report_userId_createdAt_idx" ON "Report"("userId", "createdAt" DESC);
CREATE INDEX "Report_userId_type_idx" ON "Report"("userId", "type");
CREATE INDEX "Report_status_idx" ON "Report"("status");

-- CreateIndex
CREATE UNIQUE INDEX "MacroeconomicData_province_indicator_period_key" ON "MacroeconomicData"("province", "indicator", "period");
CREATE INDEX "MacroeconomicData_province_idx" ON "MacroeconomicData"("province");
CREATE INDEX "MacroeconomicData_indicator_period_idx" ON "MacroeconomicData"("indicator", "period");

-- CreateIndex
CREATE INDEX "InfrastructureProject_province_idx" ON "InfrastructureProject"("province");
CREATE INDEX "InfrastructureProject_category_idx" ON "InfrastructureProject"("category");
CREATE INDEX "InfrastructureProject_status_idx" ON "InfrastructureProject"("status");
CREATE INDEX "InfrastructureProject_province_category_idx" ON "InfrastructureProject"("province", "category");

-- AddForeignKey
ALTER TABLE "Report" ADD CONSTRAINT "Report_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
