-- CreateEnum
CREATE TYPE "FilteringOutcome" AS ENUM ('PASSED', 'FILTERED_OUT', 'FLAGGED');

-- CreateEnum
CREATE TYPE "FilteringRuleType" AS ENUM ('FIELD_BASED', 'DOCUMENT_CHECK', 'AI_SCREENING');

-- CreateEnum
CREATE TYPE "AwardScoringMode" AS ENUM ('PICK_WINNER', 'RANKED', 'SCORED');

-- CreateEnum
CREATE TYPE "AwardStatus" AS ENUM ('DRAFT', 'NOMINATIONS_OPEN', 'VOTING_OPEN', 'CLOSED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "EligibilityMethod" AS ENUM ('AUTO', 'MANUAL');

-- AlterTable
ALTER TABLE "Project" ADD COLUMN     "foundedAt" TIMESTAMP(3);

-- AlterTable (inviteToken and inviteTokenExpiresAt already exist from previous migration)

-- CreateTable
CREATE TABLE "FilteringRule" (
    "id" TEXT NOT NULL,
    "roundId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "ruleType" "FilteringRuleType" NOT NULL,
    "configJson" JSONB NOT NULL,
    "priority" INTEGER NOT NULL DEFAULT 0,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "FilteringResult" (
    "id" TEXT NOT NULL,
    "roundId" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "outcome" "FilteringOutcome" NOT NULL,
    "ruleResultsJson" JSONB,
    "aiScreeningJson" JSONB,
    "overriddenBy" TEXT,
    "overriddenAt" TIMESTAMP(3),
    "overrideReason" TEXT,
    "finalOutcome" "FilteringOutcome",
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SpecialAward" (
    "id" TEXT NOT NULL,
    "programId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "status" "AwardStatus" NOT NULL DEFAULT 'DRAFT',
    "criteriaText" TEXT,
    "autoTagRulesJson" JSONB,
    "scoringMode" "AwardScoringMode" NOT NULL DEFAULT 'PICK_WINNER',
    "maxRankedPicks" INTEGER,
    "votingStartAt" TIMESTAMP(3),
    "votingEndAt" TIMESTAMP(3),
    "evaluationFormId" TEXT,
    "winnerProjectId" TEXT,
    "winnerOverridden" BOOLEAN NOT NULL DEFAULT false,
    "winnerOverriddenBy" TEXT,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "AwardEligibility" (
    "id" TEXT NOT NULL,
    "awardId" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "method" "EligibilityMethod" NOT NULL DEFAULT 'AUTO',
    "eligible" BOOLEAN NOT NULL DEFAULT false,
    "aiReasoningJson" JSONB,
    "overriddenBy" TEXT,
    "overriddenAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "AwardJuror" (
    "id" TEXT NOT NULL,
    "awardId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "AwardVote" (
    "id" TEXT NOT NULL,
    "awardId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "rank" INTEGER,
    "votedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "FilteringRule_roundId_idx" ON "FilteringRule"("roundId");

-- CreateIndex
CREATE INDEX "FilteringRule_priority_idx" ON "FilteringRule"("priority");

-- CreateIndex
CREATE INDEX "FilteringResult_roundId_idx" ON "FilteringResult"("roundId");

-- CreateIndex
CREATE INDEX "FilteringResult_projectId_idx" ON "FilteringResult"("projectId");

-- CreateIndex
CREATE INDEX "FilteringResult_outcome_idx" ON "FilteringResult"("outcome");

-- CreateIndex
CREATE UNIQUE INDEX "FilteringResult_roundId_projectId_key" ON "FilteringResult"("roundId", "projectId");

-- CreateIndex
CREATE INDEX "SpecialAward_programId_idx" ON "SpecialAward"("programId");

-- CreateIndex
CREATE INDEX "SpecialAward_status_idx" ON "SpecialAward"("status");

-- CreateIndex
CREATE INDEX "SpecialAward_sortOrder_idx" ON "SpecialAward"("sortOrder");

-- CreateIndex
CREATE INDEX "AwardEligibility_awardId_idx" ON "AwardEligibility"("awardId");

-- CreateIndex
CREATE INDEX "AwardEligibility_projectId_idx" ON "AwardEligibility"("projectId");

-- CreateIndex
CREATE INDEX "AwardEligibility_eligible_idx" ON "AwardEligibility"("eligible");

-- CreateIndex
CREATE UNIQUE INDEX "AwardEligibility_awardId_projectId_key" ON "AwardEligibility"("awardId", "projectId");

-- CreateIndex
CREATE INDEX "AwardJuror_awardId_idx" ON "AwardJuror"("awardId");

-- CreateIndex
CREATE INDEX "AwardJuror_userId_idx" ON "AwardJuror"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "AwardJuror_awardId_userId_key" ON "AwardJuror"("awardId", "userId");

-- CreateIndex
CREATE INDEX "AwardVote_awardId_idx" ON "AwardVote"("awardId");

-- CreateIndex
CREATE INDEX "AwardVote_userId_idx" ON "AwardVote"("userId");

-- CreateIndex
CREATE INDEX "AwardVote_projectId_idx" ON "AwardVote"("projectId");

-- CreateIndex
CREATE UNIQUE INDEX "AwardVote_awardId_userId_projectId_key" ON "AwardVote"("awardId", "userId", "projectId");

-- CreateIndex (User_inviteToken_key already exists from previous migration)

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

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

-- AddForeignKey
ALTER TABLE "FilteringResult" ADD CONSTRAINT "FilteringResult_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "FilteringResult" ADD CONSTRAINT "FilteringResult_overriddenBy_fkey" FOREIGN KEY ("overriddenBy") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SpecialAward" ADD CONSTRAINT "SpecialAward_programId_fkey" FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SpecialAward" ADD CONSTRAINT "SpecialAward_winnerProjectId_fkey" FOREIGN KEY ("winnerProjectId") REFERENCES "Project"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardEligibility" ADD CONSTRAINT "AwardEligibility_awardId_fkey" FOREIGN KEY ("awardId") REFERENCES "SpecialAward"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardEligibility" ADD CONSTRAINT "AwardEligibility_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardEligibility" ADD CONSTRAINT "AwardEligibility_overriddenBy_fkey" FOREIGN KEY ("overriddenBy") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardJuror" ADD CONSTRAINT "AwardJuror_awardId_fkey" FOREIGN KEY ("awardId") REFERENCES "SpecialAward"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardJuror" ADD CONSTRAINT "AwardJuror_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardVote" ADD CONSTRAINT "AwardVote_awardId_fkey" FOREIGN KEY ("awardId") REFERENCES "SpecialAward"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardVote" ADD CONSTRAINT "AwardVote_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardVote" ADD CONSTRAINT "AwardVote_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE;
