-- CreateTable
CREATE TABLE IF NOT EXISTS "MeetingCode" (
    "id" TEXT NOT NULL,
    "scheduleId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "roomId" TEXT NOT NULL,
    "type" TEXT NOT NULL DEFAULT 'guest',
    "code" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "MeetingCode_scheduleId_idx" ON "MeetingCode"("scheduleId");
CREATE INDEX "MeetingCode_userId_idx" ON "MeetingCode"("userId");
CREATE INDEX "MeetingCode_type_idx" ON "MeetingCode"("type");

-- CreateIndex
CREATE UNIQUE INDEX "MeetingCode_code_key" ON "MeetingCode"("code");
CREATE UNIQUE INDEX "MeetingCode_scheduleId_userId_type_key" ON "MeetingCode"("scheduleId", "userId", "type");

-- AddForeignKey
ALTER TABLE "MeetingCode" ADD CONSTRAINT "MeetingCode_scheduleId_fkey" FOREIGN KEY ("scheduleId") REFERENCES "Schedule"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "MeetingCode" ADD CONSTRAINT "MeetingCode_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- Remove hostCode and guestCode columns if they still exist
ALTER TABLE "Schedule" DROP COLUMN IF EXISTS "hostCode";
ALTER TABLE "Schedule" DROP COLUMN IF EXISTS "guestCode";
