-- CreateEnum
CREATE TYPE "RestoreStatus" AS ENUM ('PENDING', 'UPLOADING', 'RUNNING', 'COMPLETED', 'FAILED');

-- AlterEnum
ALTER TYPE "AuditAction" ADD VALUE 'BACKUP_RESTORE';

-- CreateTable
CREATE TABLE "instance_restores" (
    "id" TEXT NOT NULL,
    "instance_id" TEXT NOT NULL,
    "backup_id" TEXT NOT NULL,
    "status" "RestoreStatus" NOT NULL DEFAULT 'PENDING',
    "upload_id" TEXT,
    "progress_json" JSONB,
    "log_tail" TEXT,
    "error_message" TEXT,
    "triggered_by_id" TEXT,
    "started_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMP(3),

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

-- CreateIndex
CREATE INDEX "instance_restores_instance_id_started_at_idx" ON "instance_restores"("instance_id", "started_at");

-- CreateIndex
CREATE INDEX "instance_restores_backup_id_idx" ON "instance_restores"("backup_id");

-- AddForeignKey
ALTER TABLE "instance_restores" ADD CONSTRAINT "instance_restores_instance_id_fkey" FOREIGN KEY ("instance_id") REFERENCES "instances"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "instance_restores" ADD CONSTRAINT "instance_restores_backup_id_fkey" FOREIGN KEY ("backup_id") REFERENCES "backups"("id") ON DELETE CASCADE ON UPDATE CASCADE;
