-- CreateTable
CREATE TABLE "ScheduledTodo" (
    "id" TEXT NOT NULL,
    "title" VARCHAR(100) NOT NULL,
    "details" TEXT,
    "userId" TEXT,
    "projectId" TEXT,
    "startDate" DATE NOT NULL,
    "rrule" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ScheduledTodoCompletion" (
    "id" TEXT NOT NULL,
    "scheduledTodoId" TEXT NOT NULL,
    "occurrenceDate" DATE NOT NULL,
    "completedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "ScheduledTodo_userId_idx" ON "ScheduledTodo"("userId");

-- CreateIndex
CREATE INDEX "ScheduledTodo_projectId_idx" ON "ScheduledTodo"("projectId");

-- CreateIndex
CREATE UNIQUE INDEX "ScheduledTodoCompletion_scheduledTodoId_occurrenceDate_key" ON "ScheduledTodoCompletion"("scheduledTodoId", "occurrenceDate");

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

-- AddForeignKey
ALTER TABLE "ScheduledTodo" ADD CONSTRAINT "ScheduledTodo_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ScheduledTodoCompletion" ADD CONSTRAINT "ScheduledTodoCompletion_scheduledTodoId_fkey" FOREIGN KEY ("scheduledTodoId") REFERENCES "ScheduledTodo"("id") ON DELETE CASCADE ON UPDATE CASCADE;
