CREATE EXTENSION IF NOT EXISTS "pgcrypto";

CREATE TYPE "UserRole" AS ENUM ('ADMIN');
CREATE TYPE "UserStatus" AS ENUM ('ACTIVE', 'INACTIVE');
CREATE TYPE "MapStatus" AS ENUM ('ACTIVE', 'INACTIVE');
CREATE TYPE "UploadStatus" AS ENUM ('PENDING', 'UPLOADING', 'PROCESSING', 'COMPLETED', 'FAILED');
CREATE TYPE "JobStatus" AS ENUM ('PENDING', 'COMPLETED', 'FAILED');

CREATE TABLE "User" (
  "id" UUID NOT NULL DEFAULT gen_random_uuid(),
  "name" TEXT NOT NULL,
  "email" TEXT NOT NULL,
  "passwordHash" TEXT NOT NULL,
  "phone" TEXT,
  "avatar" TEXT,
  "role" "UserRole" NOT NULL DEFAULT 'ADMIN',
  "status" "UserStatus" NOT NULL DEFAULT 'ACTIVE',
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,
  "deletedAt" TIMESTAMP(3),
  CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "RefreshToken" (
  "id" UUID NOT NULL DEFAULT gen_random_uuid(),
  "userId" UUID NOT NULL,
  "tokenHash" TEXT NOT NULL,
  "expiresAt" TIMESTAMP(3) NOT NULL,
  "revokedAt" TIMESTAMP(3),
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "RefreshToken_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "OfflineMap" (
  "id" UUID NOT NULL DEFAULT gen_random_uuid(),
  "countryName" TEXT NOT NULL,
  "countryCode" TEXT,
  "stateName" TEXT NOT NULL,
  "s3Key" TEXT,
  "s3Url" TEXT,
  "fileName" TEXT NOT NULL,
  "fileSize" BIGINT NOT NULL,
  "mimeType" TEXT NOT NULL,
  "status" "MapStatus" NOT NULL DEFAULT 'INACTIVE',
  "uploadStatus" "UploadStatus" NOT NULL DEFAULT 'PENDING',
  "uploadedById" UUID NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,
  "deletedAt" TIMESTAMP(3),
  CONSTRAINT "OfflineMap_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "MapUploadHistory" (
  "id" UUID NOT NULL DEFAULT gen_random_uuid(),
  "offlineMapId" UUID NOT NULL,
  "oldS3Key" TEXT,
  "newS3Key" TEXT,
  "uploadId" TEXT,
  "status" "UploadStatus" NOT NULL DEFAULT 'PENDING',
  "errorMessage" TEXT,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,
  CONSTRAINT "MapUploadHistory_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "BackgroundJobLog" (
  "id" UUID NOT NULL DEFAULT gen_random_uuid(),
  "queueName" TEXT NOT NULL,
  "jobName" TEXT NOT NULL,
  "payload" JSONB NOT NULL,
  "status" "JobStatus" NOT NULL DEFAULT 'PENDING',
  "error" TEXT,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,
  CONSTRAINT "BackgroundJobLog_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
CREATE INDEX "User_deletedAt_idx" ON "User"("deletedAt");
CREATE INDEX "RefreshToken_userId_idx" ON "RefreshToken"("userId");
CREATE INDEX "OfflineMap_countryName_idx" ON "OfflineMap"("countryName");
CREATE INDEX "OfflineMap_stateName_idx" ON "OfflineMap"("stateName");
CREATE INDEX "OfflineMap_status_idx" ON "OfflineMap"("status");
CREATE INDEX "OfflineMap_uploadStatus_idx" ON "OfflineMap"("uploadStatus");
CREATE INDEX "OfflineMap_deletedAt_idx" ON "OfflineMap"("deletedAt");
CREATE INDEX "OfflineMap_uploadedById_idx" ON "OfflineMap"("uploadedById");
CREATE INDEX "OfflineMap_countryName_stateName_idx" ON "OfflineMap"("countryName", "stateName");
CREATE INDEX "OfflineMap_countryName_stateName_status_uploadStatus_deletedAt_idx" ON "OfflineMap"("countryName", "stateName", "status", "uploadStatus", "deletedAt");
CREATE INDEX "MapUploadHistory_offlineMapId_idx" ON "MapUploadHistory"("offlineMapId");
CREATE INDEX "BackgroundJobLog_queueName_idx" ON "BackgroundJobLog"("queueName");
CREATE INDEX "BackgroundJobLog_status_idx" ON "BackgroundJobLog"("status");

ALTER TABLE "RefreshToken" ADD CONSTRAINT "RefreshToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "OfflineMap" ADD CONSTRAINT "OfflineMap_uploadedById_fkey" FOREIGN KEY ("uploadedById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "MapUploadHistory" ADD CONSTRAINT "MapUploadHistory_offlineMapId_fkey" FOREIGN KEY ("offlineMapId") REFERENCES "OfflineMap"("id") ON DELETE CASCADE ON UPDATE CASCADE;
