-- CreateEnum
CREATE TYPE "RankingTriggerType" AS ENUM ('MANUAL', 'AUTO', 'RETROACTIVE', 'QUICK');

-- CreateEnum
CREATE TYPE "RankingMode" AS ENUM ('PREVIEW', 'CONFIRMED', 'QUICK');

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

-- CreateTable
CREATE TABLE "RankingSnapshot" (
    "id" TEXT NOT NULL,
    "roundId" TEXT NOT NULL,
    "triggeredById" TEXT,
    "triggerType" "RankingTriggerType" NOT NULL DEFAULT 'MANUAL',
    "criteriaText" TEXT NOT NULL,
    "parsedRulesJson" JSONB NOT NULL,
    "startupRankingJson" JSONB,
    "conceptRankingJson" JSONB,
    "evaluationDataJson" JSONB,
    "mode" "RankingMode" NOT NULL DEFAULT 'PREVIEW',
    "status" "RankingSnapshotStatus" NOT NULL DEFAULT 'COMPLETED',
    "reordersJson" JSONB,
    "model" TEXT,
    "tokensUsed" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "RankingSnapshot_roundId_idx" ON "RankingSnapshot"("roundId");

-- CreateIndex
CREATE INDEX "RankingSnapshot_triggeredById_idx" ON "RankingSnapshot"("triggeredById");

-- CreateIndex
CREATE INDEX "RankingSnapshot_createdAt_idx" ON "RankingSnapshot"("createdAt");

-- AddForeignKey
ALTER TABLE "RankingSnapshot" ADD CONSTRAINT "RankingSnapshot_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RankingSnapshot" ADD CONSTRAINT "RankingSnapshot_triggeredById_fkey" FOREIGN KEY ("triggeredById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
