-- Phase 4: Worlds & Backups

-- AlterEnum
ALTER TYPE "GameServerStatus" ADD VALUE 'BACKING_UP';
ALTER TYPE "GameServerStatus" ADD VALUE 'RESTORING';

-- CreateEnum
CREATE TYPE "BackupStatus" AS ENUM ('PENDING', 'RUNNING', 'AVAILABLE', 'FAILED', 'DELETED');
CREATE TYPE "BackupType" AS ENUM ('MANUAL', 'SCHEDULED', 'PRE_RESTORE', 'PRE_WORLD_REPLACE');
CREATE TYPE "BackupRestoreStatus" AS ENUM ('PENDING', 'RUNNING', 'COMPLETED', 'FAILED');
CREATE TYPE "WorldUploadStatus" AS ENUM ('PENDING', 'UPLOADING', 'VALIDATING', 'READY', 'COMPLETED', 'FAILED');

-- AlterTable
ALTER TABLE "game_servers" ADD COLUMN "activeWorldName" TEXT NOT NULL DEFAULT 'world';

-- CreateTable
CREATE TABLE "server_backups" (
    "id" TEXT NOT NULL,
    "serverId" TEXT NOT NULL,
    "type" "BackupType" NOT NULL DEFAULT 'MANUAL',
    "status" "BackupStatus" NOT NULL DEFAULT 'PENDING',
    "s3Key" TEXT,
    "sizeBytes" BIGINT,
    "sha256" TEXT,
    "label" TEXT,
    "failureReason" TEXT,
    "createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completedAt" TIMESTAMPTZ(3),
    "expiresAt" TIMESTAMPTZ(3),

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

CREATE TABLE "backup_restores" (
    "id" TEXT NOT NULL,
    "serverId" TEXT NOT NULL,
    "backupId" TEXT NOT NULL,
    "safetyBackupId" TEXT,
    "status" "BackupRestoreStatus" NOT NULL DEFAULT 'PENDING',
    "failureReason" TEXT,
    "createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completedAt" TIMESTAMPTZ(3),

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

CREATE TABLE "backup_schedules" (
    "id" TEXT NOT NULL,
    "serverId" TEXT NOT NULL,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "intervalHours" INTEGER NOT NULL DEFAULT 24,
    "retentionCount" INTEGER NOT NULL DEFAULT 5,
    "nextRunAt" TIMESTAMPTZ(3),
    "lastRunAt" TIMESTAMPTZ(3),
    "createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMPTZ(3) NOT NULL,

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

CREATE TABLE "world_uploads" (
    "id" TEXT NOT NULL,
    "serverId" TEXT NOT NULL,
    "status" "WorldUploadStatus" NOT NULL DEFAULT 'PENDING',
    "s3Key" TEXT NOT NULL,
    "worldName" TEXT NOT NULL DEFAULT 'world',
    "sha256" TEXT,
    "sizeBytes" BIGINT,
    "failureReason" TEXT,
    "createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completedAt" TIMESTAMPTZ(3),

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

-- CreateIndex
CREATE INDEX "server_backups_serverId_idx" ON "server_backups"("serverId");
CREATE INDEX "server_backups_status_idx" ON "server_backups"("status");
CREATE INDEX "server_backups_createdAt_idx" ON "server_backups"("createdAt");

CREATE INDEX "backup_restores_serverId_idx" ON "backup_restores"("serverId");
CREATE INDEX "backup_restores_backupId_idx" ON "backup_restores"("backupId");
CREATE INDEX "backup_restores_status_idx" ON "backup_restores"("status");

CREATE UNIQUE INDEX "backup_schedules_serverId_key" ON "backup_schedules"("serverId");

CREATE INDEX "world_uploads_serverId_idx" ON "world_uploads"("serverId");
CREATE INDEX "world_uploads_status_idx" ON "world_uploads"("status");

-- AddForeignKey
ALTER TABLE "server_backups" ADD CONSTRAINT "server_backups_serverId_fkey" FOREIGN KEY ("serverId") REFERENCES "game_servers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "backup_restores" ADD CONSTRAINT "backup_restores_serverId_fkey" FOREIGN KEY ("serverId") REFERENCES "game_servers"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "backup_restores" ADD CONSTRAINT "backup_restores_backupId_fkey" FOREIGN KEY ("backupId") REFERENCES "server_backups"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "backup_restores" ADD CONSTRAINT "backup_restores_safetyBackupId_fkey" FOREIGN KEY ("safetyBackupId") REFERENCES "server_backups"("id") ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE "backup_schedules" ADD CONSTRAINT "backup_schedules_serverId_fkey" FOREIGN KEY ("serverId") REFERENCES "game_servers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "world_uploads" ADD CONSTRAINT "world_uploads_serverId_fkey" FOREIGN KEY ("serverId") REFERENCES "game_servers"("id") ON DELETE CASCADE ON UPDATE CASCADE;
