-- CreateEnum
CREATE TYPE "POIType" AS ENUM (
  'SCHOOL', 'UNIVERSITY', 'HOSPITAL', 'CLINIC',
  'METRO_STATION', 'BUS_STOP',
  'MALL', 'MARKET', 'SUPERMARKET',
  'PARK',
  'POLICE_STATION', 'FIRE_STATION',
  'BANK', 'ATM',
  'RESTAURANT', 'CAFE', 'GYM', 'PHARMACY'
);

-- CreateTable: POI (Points of Interest)
CREATE TABLE "POI" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "POIType" NOT NULL,
    "location" geometry(Point, 4326) NOT NULL,
    "address" TEXT,
    "ward" TEXT,
    "district" TEXT NOT NULL,
    "city" TEXT NOT NULL,
    "osmId" TEXT,
    "metadata" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: NeighborhoodScore (cached scoring per district)
CREATE TABLE "NeighborhoodScore" (
    "id" TEXT NOT NULL,
    "district" TEXT NOT NULL,
    "city" TEXT NOT NULL,
    "educationScore" DOUBLE PRECISION NOT NULL,
    "healthcareScore" DOUBLE PRECISION NOT NULL,
    "transportScore" DOUBLE PRECISION NOT NULL,
    "shoppingScore" DOUBLE PRECISION NOT NULL,
    "greeneryScore" DOUBLE PRECISION NOT NULL,
    "safetyScore" DOUBLE PRECISION NOT NULL,
    "totalScore" DOUBLE PRECISION NOT NULL,
    "poiCounts" JSONB NOT NULL,
    "calculatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- POI Indexes
CREATE UNIQUE INDEX "POI_osmId_key" ON "POI"("osmId");
CREATE INDEX "POI_type_idx" ON "POI"("type");
CREATE INDEX "POI_district_city_idx" ON "POI"("district", "city");
CREATE INDEX "POI_type_district_city_idx" ON "POI"("type", "district", "city");
CREATE INDEX "POI_location_idx" ON "POI" USING GIST ("location");
CREATE INDEX "POI_osmId_idx" ON "POI"("osmId");

-- NeighborhoodScore Indexes
CREATE UNIQUE INDEX "NeighborhoodScore_district_city_key" ON "NeighborhoodScore"("district", "city");
CREATE INDEX "NeighborhoodScore_totalScore_idx" ON "NeighborhoodScore"("totalScore" DESC);
CREATE INDEX "NeighborhoodScore_city_idx" ON "NeighborhoodScore"("city");
