-- Phase 5: Software & Add-ons

CREATE TYPE "AddonType" AS ENUM ('PLUGIN', 'MOD', 'MODPACK', 'DATAPACK');
CREATE TYPE "AddonInstallStatus" AS ENUM ('PENDING', 'INSTALLING', 'INSTALLED', 'FAILED', 'REMOVING', 'REMOVED');

CREATE TABLE "installed_addons" (
    "id" TEXT NOT NULL,
    "serverId" TEXT NOT NULL,
    "provider" TEXT NOT NULL DEFAULT 'modrinth',
    "projectId" TEXT NOT NULL,
    "projectSlug" TEXT NOT NULL,
    "projectName" TEXT NOT NULL,
    "versionId" TEXT NOT NULL,
    "versionNumber" TEXT NOT NULL,
    "addonType" "AddonType" NOT NULL,
    "fileName" TEXT NOT NULL,
    "filePath" TEXT NOT NULL,
    "fileSha512" TEXT,
    "status" "AddonInstallStatus" NOT NULL DEFAULT 'PENDING',
    "failureReason" TEXT,
    "installedAt" TIMESTAMPTZ(3),
    "createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMPTZ(3) NOT NULL,

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

CREATE INDEX "installed_addons_serverId_idx" ON "installed_addons"("serverId");
CREATE INDEX "installed_addons_status_idx" ON "installed_addons"("status");
CREATE UNIQUE INDEX "installed_addons_serverId_projectId_versionId_key" ON "installed_addons"("serverId", "projectId", "versionId");

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