-- CreateEnum
CREATE TYPE "VisitOutcome" AS ENUM ('NOT_HOME', 'REFUSED', 'MOVED', 'ALREADY_VOTED', 'SPOKE_WITH', 'LEFT_LITERATURE', 'COME_BACK_LATER');

-- CreateEnum
CREATE TYPE "CanvassSessionStatus" AS ENUM ('ACTIVE', 'COMPLETED', 'ABANDONED');

-- AlterTable
ALTER TABLE "shifts" ADD COLUMN     "cutId" TEXT;

-- CreateTable
CREATE TABLE "canvass_sessions" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "cutId" TEXT NOT NULL,
    "shiftId" TEXT,
    "status" "CanvassSessionStatus" NOT NULL DEFAULT 'ACTIVE',
    "startedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "endedAt" TIMESTAMP(3),
    "startLatitude" DECIMAL(10,8),
    "startLongitude" DECIMAL(11,8),

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

-- CreateTable
CREATE TABLE "canvass_visits" (
    "id" TEXT NOT NULL,
    "locationId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "shiftId" TEXT,
    "sessionId" TEXT,
    "outcome" "VisitOutcome" NOT NULL,
    "supportLevel" "SupportLevel",
    "signRequested" BOOLEAN NOT NULL DEFAULT false,
    "signSize" TEXT,
    "notes" TEXT,
    "durationSeconds" INTEGER,
    "visitedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "canvass_sessions_userId_idx" ON "canvass_sessions"("userId");

-- CreateIndex
CREATE INDEX "canvass_sessions_cutId_idx" ON "canvass_sessions"("cutId");

-- CreateIndex
CREATE INDEX "canvass_sessions_shiftId_idx" ON "canvass_sessions"("shiftId");

-- CreateIndex
CREATE INDEX "canvass_visits_locationId_idx" ON "canvass_visits"("locationId");

-- CreateIndex
CREATE INDEX "canvass_visits_userId_idx" ON "canvass_visits"("userId");

-- CreateIndex
CREATE INDEX "canvass_visits_shiftId_idx" ON "canvass_visits"("shiftId");

-- CreateIndex
CREATE INDEX "canvass_visits_sessionId_idx" ON "canvass_visits"("sessionId");

-- CreateIndex
CREATE INDEX "canvass_visits_visitedAt_idx" ON "canvass_visits"("visitedAt");

-- CreateIndex
CREATE INDEX "shifts_cutId_idx" ON "shifts"("cutId");

-- AddForeignKey
ALTER TABLE "shifts" ADD CONSTRAINT "shifts_cutId_fkey" FOREIGN KEY ("cutId") REFERENCES "cuts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_sessions" ADD CONSTRAINT "canvass_sessions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_sessions" ADD CONSTRAINT "canvass_sessions_cutId_fkey" FOREIGN KEY ("cutId") REFERENCES "cuts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_sessions" ADD CONSTRAINT "canvass_sessions_shiftId_fkey" FOREIGN KEY ("shiftId") REFERENCES "shifts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_visits" ADD CONSTRAINT "canvass_visits_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "locations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_visits" ADD CONSTRAINT "canvass_visits_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_visits" ADD CONSTRAINT "canvass_visits_shiftId_fkey" FOREIGN KEY ("shiftId") REFERENCES "shifts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_visits" ADD CONSTRAINT "canvass_visits_sessionId_fkey" FOREIGN KEY ("sessionId") REFERENCES "canvass_sessions"("id") ON DELETE SET NULL ON UPDATE CASCADE;
