-- AlterTable: Rename phoneNumber to phoneWork (preserves existing data)
ALTER TABLE "EmployeeProfile" RENAME COLUMN "phoneNumber" TO "phoneWork";

-- AlterTable: Add new phone columns
ALTER TABLE "EmployeeProfile" ADD COLUMN "phoneMobile" TEXT;
ALTER TABLE "EmployeeProfile" ADD COLUMN "phoneEmergency" TEXT;

-- AlterTable: Add addressId to WorkLocation
ALTER TABLE "WorkLocation" ADD COLUMN "addressId" TEXT;

-- CreateTable
CREATE TABLE "Country" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "iso2" TEXT NOT NULL,
    "iso3" TEXT NOT NULL,
    "numericCode" TEXT NOT NULL,
    "phoneCode" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Region" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT,
    "countryId" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "City" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "regionId" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Address" (
    "id" TEXT NOT NULL,
    "label" TEXT NOT NULL,
    "addressLine1" TEXT NOT NULL,
    "addressLine2" TEXT,
    "cityId" TEXT NOT NULL,
    "postalCode" TEXT NOT NULL,
    "latitude" DECIMAL(10,7),
    "longitude" DECIMAL(10,7),
    "validatedAt" TIMESTAMP(3),
    "validationSource" TEXT,
    "status" TEXT NOT NULL DEFAULT 'active',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "EmployeeAddress" (
    "id" TEXT NOT NULL,
    "employeeProfileId" TEXT NOT NULL,
    "addressId" TEXT NOT NULL,
    "isPrimary" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "Country_iso2_key" ON "Country"("iso2");

-- CreateIndex
CREATE UNIQUE INDEX "Country_iso3_key" ON "Country"("iso3");

-- CreateIndex
CREATE INDEX "Country_status_idx" ON "Country"("status");

-- CreateIndex
CREATE INDEX "Region_countryId_idx" ON "Region"("countryId");

-- CreateIndex
CREATE INDEX "Region_status_idx" ON "Region"("status");

-- CreateIndex (non-unique; uniqueness handled by case-insensitive functional index below)
CREATE INDEX "Region_countryId_name_idx" ON "Region"("countryId", "name");

-- CreateIndex
CREATE INDEX "City_regionId_idx" ON "City"("regionId");

-- CreateIndex
CREATE INDEX "City_status_idx" ON "City"("status");

-- CreateIndex (non-unique; uniqueness handled by case-insensitive functional index below)
CREATE INDEX "City_regionId_name_idx" ON "City"("regionId", "name");

-- CreateIndex
CREATE INDEX "Address_cityId_idx" ON "Address"("cityId");

-- CreateIndex
CREATE INDEX "Address_status_idx" ON "Address"("status");

-- CreateIndex
CREATE INDEX "EmployeeAddress_employeeProfileId_idx" ON "EmployeeAddress"("employeeProfileId");

-- CreateIndex
CREATE INDEX "EmployeeAddress_addressId_idx" ON "EmployeeAddress"("addressId");

-- CreateIndex
CREATE UNIQUE INDEX "EmployeeAddress_employeeProfileId_addressId_key" ON "EmployeeAddress"("employeeProfileId", "addressId");

-- CreateIndex
CREATE INDEX "WorkLocation_addressId_idx" ON "WorkLocation"("addressId");

-- AddForeignKey
ALTER TABLE "WorkLocation" ADD CONSTRAINT "WorkLocation_addressId_fkey" FOREIGN KEY ("addressId") REFERENCES "Address"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Region" ADD CONSTRAINT "Region_countryId_fkey" FOREIGN KEY ("countryId") REFERENCES "Country"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "City" ADD CONSTRAINT "City_regionId_fkey" FOREIGN KEY ("regionId") REFERENCES "Region"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Address" ADD CONSTRAINT "Address_cityId_fkey" FOREIGN KEY ("cityId") REFERENCES "City"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EmployeeAddress" ADD CONSTRAINT "EmployeeAddress_employeeProfileId_fkey" FOREIGN KEY ("employeeProfileId") REFERENCES "EmployeeProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EmployeeAddress" ADD CONSTRAINT "EmployeeAddress_addressId_fkey" FOREIGN KEY ("addressId") REFERENCES "Address"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- Unique index on Country.numericCode (ISO 3166-1 numeric)
CREATE UNIQUE INDEX "Country_numericCode_key" ON "Country"("numericCode");

-- Case-insensitive functional unique indexes
CREATE UNIQUE INDEX "Region_countryId_name_ci" ON "Region" (LOWER("name"), "countryId");
CREATE UNIQUE INDEX "City_regionId_name_ci" ON "City" (LOWER("name"), "regionId");

-- Partial unique index: at most one primary address per employee
CREATE UNIQUE INDEX "EmployeeAddress_employeeProfileId_primary_key"
    ON "EmployeeAddress" ("employeeProfileId")
    WHERE "isPrimary" = true;

-- CHECK constraint: restrict Address.label to known values
ALTER TABLE "Address" ADD CONSTRAINT "Address_label_check"
    CHECK ("label" IN ('home', 'work', 'billing', 'shipping', 'headquarters', 'site'));
