CREATE TYPE "Privacy" AS ENUM ('PUBLIC', 'PRIVATE', 'UNLISTED');
CREATE TYPE "JobStatus" AS ENUM ('PENDING', 'PROCESSING', 'COMPLETED', 'FAILED', 'PARTIAL');
CREATE TYPE "JobItemStatus" AS ENUM ('PENDING', 'ENCODING', 'UPLOADING', 'COMPLETED', 'FAILED');

CREATE TABLE "User" (
  "id" TEXT NOT NULL,
  "email" TEXT NOT NULL,
  "name" TEXT,
  "image" TEXT,
  "createdVideoCount" INTEGER NOT NULL DEFAULT 0,
  "apiKeyHash" TEXT,
  "apiKeyPrefix" TEXT,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "YouTubeConnection" (
  "id" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "accessToken" TEXT NOT NULL,
  "refreshToken" TEXT NOT NULL,
  "expiresAt" TIMESTAMP(3) NOT NULL,
  "channelId" TEXT NOT NULL,
  "channelTitle" TEXT NOT NULL,
  CONSTRAINT "YouTubeConnection_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "Job" (
  "id" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "status" "JobStatus" NOT NULL DEFAULT 'PENDING',
  "imagePath" TEXT NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "completedAt" TIMESTAMP(3),
  CONSTRAINT "Job_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "JobItem" (
  "id" TEXT NOT NULL,
  "jobId" TEXT NOT NULL,
  "audioPath" TEXT NOT NULL,
  "audioFilename" TEXT NOT NULL,
  "title" TEXT NOT NULL,
  "description" TEXT NOT NULL DEFAULT '',
  "tags" TEXT NOT NULL DEFAULT '',
  "privacy" "Privacy" NOT NULL DEFAULT 'PUBLIC',
  "categoryId" TEXT NOT NULL DEFAULT '10',
  "resolution" TEXT NOT NULL DEFAULT '1280x720',
  "notifySubscribers" BOOLEAN NOT NULL DEFAULT true,
  "madeForKids" BOOLEAN NOT NULL DEFAULT false,
  "embeddable" BOOLEAN NOT NULL DEFAULT true,
  "creativeCommons" BOOLEAN NOT NULL DEFAULT false,
  "includeWatermark" BOOLEAN NOT NULL DEFAULT false,
  "itemImagePath" TEXT,
  "watermarkMode" TEXT NOT NULL DEFAULT 'none',
  "watermarkText" TEXT,
  "watermarkLogoPath" TEXT,
  "watermarkFontKey" TEXT,
  "watermarkFontPath" TEXT,
  "watermarkPosition" TEXT NOT NULL DEFAULT 'bottom-right',
  "watermarkOffsetX" INTEGER NOT NULL DEFAULT 20,
  "watermarkOffsetY" INTEGER NOT NULL DEFAULT 20,
  "artist" TEXT,
  "songTitle" TEXT,
  "layoutTemplate" TEXT,
  "blurAmount" INTEGER NOT NULL DEFAULT 55,
  "blurOpacity" INTEGER NOT NULL DEFAULT 100,
  "textPadding" INTEGER NOT NULL DEFAULT 48,
  "titleArtistGap" INTEGER NOT NULL DEFAULT 10,
  "textOffsetX" INTEGER NOT NULL DEFAULT 0,
  "textOffsetY" INTEGER NOT NULL DEFAULT 0,
  "playlistId" TEXT,
  "status" "JobItemStatus" NOT NULL DEFAULT 'PENDING',
  "outputPath" TEXT,
  "youtubeVideoId" TEXT,
  "error" TEXT,
  CONSTRAINT "JobItem_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
CREATE UNIQUE INDEX "User_apiKeyHash_key" ON "User"("apiKeyHash");
CREATE UNIQUE INDEX "YouTubeConnection_userId_key" ON "YouTubeConnection"("userId");
ALTER TABLE "YouTubeConnection" ADD CONSTRAINT "YouTubeConnection_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "Job" ADD CONSTRAINT "Job_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "JobItem" ADD CONSTRAINT "JobItem_jobId_fkey"
  FOREIGN KEY ("jobId") REFERENCES "Job"("id") ON DELETE CASCADE ON UPDATE CASCADE;
