-- CreateEnum
CREATE TYPE "MonitoredReleaseState" AS ENUM ('wanted', 'grabbed', 'fulfilled');

-- AlterTable
ALTER TABLE "Request" ADD COLUMN     "monitoredReleaseId" TEXT;

-- CreateTable
CREATE TABLE "WatchedArtist" (
    "id" TEXT NOT NULL,
    "mbid" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "autoMonitorFuture" BOOLEAN NOT NULL DEFAULT false,
    "monitorFrom" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "lastPolledAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "MonitoredRelease" (
    "id" TEXT NOT NULL,
    "watchedArtistId" TEXT,
    "artistMbid" TEXT NOT NULL,
    "artistName" TEXT NOT NULL,
    "rgMbid" TEXT NOT NULL,
    "album" TEXT NOT NULL,
    "primaryType" TEXT,
    "secondaryTypes" TEXT[],
    "firstReleaseDate" TEXT,
    "monitored" BOOLEAN NOT NULL DEFAULT false,
    "state" "MonitoredReleaseState" NOT NULL DEFAULT 'wanted',
    "currentQualityClass" INTEGER,
    "firstGrabbedAt" TIMESTAMP(3),
    "lastSearchedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "WatchedArtist_mbid_key" ON "WatchedArtist"("mbid");

-- CreateIndex
CREATE UNIQUE INDEX "MonitoredRelease_rgMbid_key" ON "MonitoredRelease"("rgMbid");

-- AddForeignKey
ALTER TABLE "Request" ADD CONSTRAINT "Request_monitoredReleaseId_fkey" FOREIGN KEY ("monitoredReleaseId") REFERENCES "MonitoredRelease"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MonitoredRelease" ADD CONSTRAINT "MonitoredRelease_watchedArtistId_fkey" FOREIGN KEY ("watchedArtistId") REFERENCES "WatchedArtist"("id") ON DELETE CASCADE ON UPDATE CASCADE;
