-- CreateEnum
CREATE TYPE "PetitionStatus" AS ENUM ('DRAFT', 'ACTIVE', 'PAUSED', 'CLOSED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "PetitionSignatureStatus" AS ENUM ('PENDING_VERIFICATION', 'VERIFIED', 'UNVERIFIED', 'REJECTED');

-- AlterEnum
ALTER TYPE "ContactActivityType" ADD VALUE 'PETITION_SIGNED';

-- AlterEnum
ALTER TYPE "ContactSource" ADD VALUE 'PETITION_SIGNER';

-- AlterTable
ALTER TABLE "site_settings" ADD COLUMN     "enable_petitions" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "notify_admin_petition_milestone" BOOLEAN NOT NULL DEFAULT false;

-- CreateTable
CREATE TABLE "petitions" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "signatureGoal" INTEGER,
    "showProgress" BOOLEAN NOT NULL DEFAULT true,
    "showSignatureCount" BOOLEAN NOT NULL DEFAULT true,
    "showSignerNames" BOOLEAN NOT NULL DEFAULT true,
    "signatureCountOffset" INTEGER NOT NULL DEFAULT 0,
    "requireName" BOOLEAN NOT NULL DEFAULT true,
    "requireEmail" BOOLEAN NOT NULL DEFAULT true,
    "requirePostalCode" BOOLEAN NOT NULL DEFAULT false,
    "requirePhone" BOOLEAN NOT NULL DEFAULT false,
    "allowComment" BOOLEAN NOT NULL DEFAULT true,
    "commentLabel" TEXT,
    "requireEmailConfirmation" BOOLEAN NOT NULL DEFAULT false,
    "confirmationEmailSubject" TEXT,
    "confirmationEmailBody" TEXT,
    "coverPhoto" TEXT,
    "callToAction" TEXT,
    "thankYouMessage" TEXT,
    "highlightPetition" BOOLEAN NOT NULL DEFAULT false,
    "linkedCampaignId" TEXT,
    "status" "PetitionStatus" NOT NULL DEFAULT 'DRAFT',
    "isUserGenerated" BOOLEAN NOT NULL DEFAULT false,
    "moderationStatus" "CampaignModerationStatus",
    "rejectionReason" TEXT,
    "moderationNotes" TEXT,
    "createdByUserId" TEXT,
    "createdByUserEmail" TEXT,
    "createdByUserName" TEXT,
    "reviewedByUserId" TEXT,
    "reviewedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "petition_signatures" (
    "id" TEXT NOT NULL,
    "petitionId" TEXT NOT NULL,
    "signerName" TEXT,
    "signerEmail" TEXT,
    "signerPostalCode" TEXT,
    "signerPhone" TEXT,
    "signerComment" TEXT,
    "isAnonymous" BOOLEAN NOT NULL DEFAULT false,
    "displayName" TEXT,
    "status" "PetitionSignatureStatus" NOT NULL DEFAULT 'UNVERIFIED',
    "verificationToken" TEXT,
    "verificationSentAt" TIMESTAMP(3),
    "verifiedAt" TIMESTAMP(3),
    "contactId" TEXT,
    "signerIp" TEXT,
    "geoCountry" TEXT,
    "geoRegion" TEXT,
    "geoCity" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "petitions_slug_key" ON "petitions"("slug");

-- CreateIndex
CREATE INDEX "petitions_status_idx" ON "petitions"("status");

-- CreateIndex
CREATE INDEX "petitions_isUserGenerated_idx" ON "petitions"("isUserGenerated");

-- CreateIndex
CREATE INDEX "petitions_highlightPetition_idx" ON "petitions"("highlightPetition");

-- CreateIndex
CREATE INDEX "petitions_linkedCampaignId_idx" ON "petitions"("linkedCampaignId");

-- CreateIndex
CREATE UNIQUE INDEX "petition_signatures_verificationToken_key" ON "petition_signatures"("verificationToken");

-- CreateIndex
CREATE INDEX "petition_signatures_petitionId_idx" ON "petition_signatures"("petitionId");

-- CreateIndex
CREATE INDEX "petition_signatures_signerEmail_idx" ON "petition_signatures"("signerEmail");

-- CreateIndex
CREATE INDEX "petition_signatures_petitionId_status_idx" ON "petition_signatures"("petitionId", "status");

-- CreateIndex
CREATE INDEX "petition_signatures_contactId_idx" ON "petition_signatures"("contactId");

-- CreateIndex
CREATE UNIQUE INDEX "petition_signatures_petitionId_signerEmail_key" ON "petition_signatures"("petitionId", "signerEmail");

-- AddForeignKey
ALTER TABLE "petitions" ADD CONSTRAINT "petitions_linkedCampaignId_fkey" FOREIGN KEY ("linkedCampaignId") REFERENCES "campaigns"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "petitions" ADD CONSTRAINT "petitions_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "petitions" ADD CONSTRAINT "petitions_reviewedByUserId_fkey" FOREIGN KEY ("reviewedByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "petition_signatures" ADD CONSTRAINT "petition_signatures_petitionId_fkey" FOREIGN KEY ("petitionId") REFERENCES "petitions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "petition_signatures" ADD CONSTRAINT "petition_signatures_contactId_fkey" FOREIGN KEY ("contactId") REFERENCES "contacts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

