-- CreateTable
CREATE TABLE "User" (
    "id" TEXT NOT NULL,
    "accountNumber" SERIAL NOT NULL,
    "email" TEXT NOT NULL,
    "passwordHash" TEXT,
    "name" TEXT,
    "image" TEXT,
    "emailVerified" TIMESTAMP(3),
    "verifiedAt" TIMESTAMP(3),
    "emailVerificationToken" TEXT,
    "emailVerificationExpires" TIMESTAMP(3),
    "passwordResetToken" TEXT,
    "passwordResetExpires" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "timezone" TEXT NOT NULL DEFAULT 'UTC',
    "autoRolling" BOOLEAN NOT NULL DEFAULT false,
    "protectEventTimes" BOOLEAN NOT NULL DEFAULT false,
    "language" TEXT NOT NULL DEFAULT 'de',
    "dateFormat" TEXT NOT NULL DEFAULT 'yyyy-MM-dd',
    "timeFormat" TEXT NOT NULL DEFAULT '24h',
    "startHour" INTEGER NOT NULL DEFAULT 8,
    "endHour" INTEGER NOT NULL DEFAULT 18,
    "showNextTask" BOOLEAN NOT NULL DEFAULT false,
    "calendarEditMode" BOOLEAN NOT NULL DEFAULT false,
    "focusTimerDuration" INTEGER NOT NULL DEFAULT 25,
    "focusBreakDuration" INTEGER NOT NULL DEFAULT 5,
    "showTimeGrid" BOOLEAN NOT NULL DEFAULT true,
    "showSomeday" BOOLEAN NOT NULL DEFAULT true,
    "showAllDayEvents" BOOLEAN NOT NULL DEFAULT true,
    "showSchedule" BOOLEAN NOT NULL DEFAULT true,
    "cellDuration" INTEGER NOT NULL DEFAULT 30,
    "viewStyle" TEXT NOT NULL DEFAULT 'simple',
    "viewDays" INTEGER NOT NULL DEFAULT 7,
    "fontSize" TEXT NOT NULL DEFAULT 'M',
    "goalFallbackType" TEXT NOT NULL DEFAULT 'quote',
    "goalDefaultSentence" TEXT NOT NULL DEFAULT 'goal of the week',
    "goalFontFamily" TEXT DEFAULT 'Lato',
    "goalFontSize" TEXT DEFAULT '1rem',
    "goalFontWeight" TEXT DEFAULT '500',
    "headlineFont" TEXT NOT NULL DEFAULT 'Oswald',
    "headlineFontSize" TEXT DEFAULT '1.5rem',
    "headlineFontWeight" TEXT DEFAULT '900',
    "dateFontFamily" TEXT DEFAULT 'Inter',
    "dateFontSize" TEXT DEFAULT '0.65rem',
    "dateFontWeight" TEXT DEFAULT '400',
    "timeTaskFontFamily" TEXT DEFAULT 'Inter',
    "timeTaskFontSize" TEXT DEFAULT '0.75rem',
    "timeTaskFontWeight" TEXT DEFAULT '500',
    "bodyFont" TEXT NOT NULL DEFAULT 'Inter',
    "taskFontFamily" TEXT DEFAULT 'Inter',
    "taskFontSize" TEXT DEFAULT '0.9rem',
    "taskFontWeight" TEXT DEFAULT '400',
    "eventFontFamily" TEXT DEFAULT 'Inter',
    "eventFontSize" TEXT DEFAULT '0.85rem',
    "eventFontWeight" TEXT DEFAULT '400',
    "fontWeight" TEXT NOT NULL DEFAULT '400',
    "weekdayColor" TEXT DEFAULT '#0ea5e9',
    "dateColor" TEXT DEFAULT '#888888',
    "taskColor" TEXT DEFAULT '#333333',
    "todayHighlightColor" TEXT DEFAULT '#f0fafa',
    "weekendColorSat" TEXT DEFAULT '#ffc107',
    "weekendColorSun" TEXT DEFAULT '#dc2626',
    "pastDayColor" TEXT DEFAULT '#a6a6a7',
    "goalScope" TEXT NOT NULL DEFAULT 'week',
    "dateLayout" TEXT NOT NULL DEFAULT 'right',
    "dateAlignment" TEXT NOT NULL DEFAULT 'center',
    "hourLabelFormat" TEXT NOT NULL DEFAULT 'short',
    "showSubHourSlots" BOOLEAN NOT NULL DEFAULT true,
    "dayHeaderGap" TEXT DEFAULT '0.75em',
    "allDayPosition" TEXT NOT NULL DEFAULT 'above',
    "cwColor" TEXT DEFAULT '#333333',
    "cwFontFamily" TEXT DEFAULT 'Oswald',
    "cwFontSize" TEXT DEFAULT '1.5rem',
    "cwFontWeight" TEXT DEFAULT '700',
    "yearColor" TEXT DEFAULT '#333333',
    "yearFontFamily" TEXT DEFAULT 'Oswald',
    "yearFontSize" TEXT DEFAULT '1.5rem',
    "yearFontWeight" TEXT DEFAULT '700',
    "showTaskCheckboxes" BOOLEAN NOT NULL DEFAULT false,
    "showCompletedTasks" BOOLEAN NOT NULL DEFAULT true,
    "showLines" BOOLEAN NOT NULL DEFAULT true,
    "startDayOffset" INTEGER NOT NULL DEFAULT -1,
    "quoteSourceUrls" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "emailVerificationCode" TEXT,

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

-- CreateTable
CREATE TABLE "Account" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "providerAccountId" TEXT NOT NULL,
    "refresh_token" TEXT,
    "access_token" TEXT,
    "expires_at" INTEGER,
    "token_type" TEXT,
    "scope" TEXT,
    "id_token" TEXT,
    "session_state" TEXT,
    "ext_expires_in" INTEGER,

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

-- CreateTable
CREATE TABLE "Session" (
    "id" TEXT NOT NULL,
    "sessionToken" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "expires" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "VerificationToken" (
    "identifier" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "expires" TIMESTAMP(3) NOT NULL
);

-- CreateTable
CREATE TABLE "Task" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "markdownContent" TEXT,
    "completed" BOOLEAN NOT NULL DEFAULT false,
    "isRolling" BOOLEAN NOT NULL DEFAULT false,
    "order" INTEGER NOT NULL DEFAULT 0,
    "dayOfWeek" INTEGER,
    "scheduledDate" TIMESTAMP(3),
    "somedayListId" TEXT,
    "originalDate" TIMESTAMP(3),
    "startTime" TEXT,
    "endTime" TEXT,
    "isRecurring" BOOLEAN NOT NULL DEFAULT false,
    "recurrenceInterval" INTEGER,
    "recurrenceUnit" TEXT,
    "recurrenceTime" TEXT,
    "recurrenceEndDate" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "externalId" TEXT,
    "externalProvider" TEXT,
    "externalListId" TEXT,
    "lastSyncedAt" TIMESTAMP(3),
    "deletedAt" TIMESTAMP(3),
    "parentTaskId" TEXT,
    "projectId" TEXT,
    "somedaySlotIndex" INTEGER,

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

-- CreateTable
CREATE TABLE "SomedayList" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "order" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "externalId" TEXT,
    "externalProvider" TEXT,
    "lastSyncedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "CalendarConnection" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "accessToken" TEXT NOT NULL,
    "refreshToken" TEXT,
    "expiresAt" TIMESTAMP(3),
    "calendars" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "CachedCalendarEvent" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "externalId" TEXT NOT NULL,
    "connectionId" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "calendarId" TEXT NOT NULL,
    "calendarTitle" TEXT NOT NULL,
    "calendarColor" TEXT,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "location" TEXT,
    "url" TEXT,
    "recurringEventId" TEXT,
    "isRecurring" BOOLEAN NOT NULL DEFAULT false,
    "startDateTime" TIMESTAMP(3),
    "startDate" TEXT,
    "endDateTime" TIMESTAMP(3),
    "endDate" TEXT,
    "weekStart" TIMESTAMP(3) NOT NULL,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "WeeklyGoal" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "weekStart" TIMESTAMP(3) NOT NULL,
    "text" TEXT NOT NULL DEFAULT 'your goal of this week',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Project" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "icon" TEXT,
    "color" TEXT,
    "description" TEXT,
    "order" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "User_accountNumber_key" ON "User"("accountNumber");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId");

-- CreateIndex
CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token");

-- CreateIndex
CREATE INDEX "Task_userId_dayOfWeek_idx" ON "Task"("userId", "dayOfWeek");

-- CreateIndex
CREATE INDEX "Task_userId_scheduledDate_idx" ON "Task"("userId", "scheduledDate");

-- CreateIndex
CREATE INDEX "Task_userId_somedayListId_idx" ON "Task"("userId", "somedayListId");

-- CreateIndex
CREATE INDEX "Task_userId_externalId_idx" ON "Task"("userId", "externalId");

-- CreateIndex
CREATE INDEX "Task_userId_deletedAt_idx" ON "Task"("userId", "deletedAt");

-- CreateIndex
CREATE INDEX "Task_userId_projectId_idx" ON "Task"("userId", "projectId");

-- CreateIndex
CREATE INDEX "Task_parentTaskId_idx" ON "Task"("parentTaskId");

-- CreateIndex
CREATE INDEX "SomedayList_userId_idx" ON "SomedayList"("userId");

-- CreateIndex
CREATE INDEX "CachedCalendarEvent_userId_startDateTime_idx" ON "CachedCalendarEvent"("userId", "startDateTime");

-- CreateIndex
CREATE INDEX "CachedCalendarEvent_userId_startDate_idx" ON "CachedCalendarEvent"("userId", "startDate");

-- CreateIndex
CREATE INDEX "CachedCalendarEvent_connectionId_weekStart_idx" ON "CachedCalendarEvent"("connectionId", "weekStart");

-- CreateIndex
CREATE UNIQUE INDEX "CachedCalendarEvent_userId_externalId_provider_key" ON "CachedCalendarEvent"("userId", "externalId", "provider");

-- CreateIndex
CREATE INDEX "WeeklyGoal_userId_idx" ON "WeeklyGoal"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "WeeklyGoal_userId_weekStart_key" ON "WeeklyGoal"("userId", "weekStart");

-- CreateIndex
CREATE INDEX "Project_userId_idx" ON "Project"("userId");

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

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

-- AddForeignKey
ALTER TABLE "Task" ADD CONSTRAINT "Task_parentTaskId_fkey" FOREIGN KEY ("parentTaskId") REFERENCES "Task"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Task" ADD CONSTRAINT "Task_somedayListId_fkey" FOREIGN KEY ("somedayListId") REFERENCES "SomedayList"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Task" ADD CONSTRAINT "Task_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

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

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

-- AddForeignKey
ALTER TABLE "CachedCalendarEvent" ADD CONSTRAINT "CachedCalendarEvent_connectionId_fkey" FOREIGN KEY ("connectionId") REFERENCES "CalendarConnection"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

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

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

