-- AlterEnum
ALTER TYPE "LedgerType" ADD VALUE 'DEBIT_POLL_VOTE';

-- CreateTable
CREATE TABLE "PollVote" (
    "id" TEXT NOT NULL,
    "pollSlug" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "displayName" TEXT NOT NULL,
    "normalizedKey" TEXT NOT NULL,
    "creditsSpent" INTEGER NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "PollVote_pollSlug_userId_key" ON "PollVote"("pollSlug", "userId");

-- CreateIndex
CREATE INDEX "PollVote_pollSlug_normalizedKey_idx" ON "PollVote"("pollSlug", "normalizedKey");

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

-- AlterTable
ALTER TABLE "LedgerEntry" ADD COLUMN "pollVoteId" TEXT;

-- CreateIndex
CREATE UNIQUE INDEX "LedgerEntry_pollVoteId_key" ON "LedgerEntry"("pollVoteId");

-- AddForeignKey
ALTER TABLE "LedgerEntry" ADD CONSTRAINT "LedgerEntry_pollVoteId_fkey" FOREIGN KEY ("pollVoteId") REFERENCES "PollVote"("id") ON DELETE SET NULL ON UPDATE CASCADE;
