-- DropIndex
DROP INDEX "GuestInvite_eventId_idx";

-- AlterTable
ALTER TABLE "Event" ADD COLUMN     "timezone" TEXT NOT NULL DEFAULT 'UTC';

-- AlterTable: nullable first so existing rows can be backfilled, then
-- tightened to NOT NULL - a plain NOT NULL ADD COLUMN would fail against
-- any pre-existing EventCollaborator rows.
ALTER TABLE "EventCollaborator" ADD COLUMN     "expiresAt" TIMESTAMP(3);
UPDATE "EventCollaborator" SET "expiresAt" = "createdAt" + INTERVAL '7 days' WHERE "expiresAt" IS NULL;
ALTER TABLE "EventCollaborator" ALTER COLUMN "expiresAt" SET NOT NULL;

-- Deduplicate before adding the unique constraint below, keeping the most
-- recent invite per (eventId, email) - defensive in case any duplicates
-- accumulated before this constraint existed.
DELETE FROM "GuestInvite" a USING "GuestInvite" b
  WHERE a."eventId" = b."eventId" AND a.email = b.email AND a."sentAt" < b."sentAt";

-- CreateIndex
CREATE UNIQUE INDEX "GuestInvite_eventId_email_key" ON "GuestInvite"("eventId", "email");
