import { redirect } from "next/navigation"

import { requireAuth } from "@/shared/lib/auth-guard"
import { AdminSettingsView } from "@/modules/settings/components/admin-settings-view"
import { StudentSettingsView } from "@/modules/settings/components/student-settings-view"
import { TeacherSettingsView } from "@/modules/settings/components/teacher-settings-view"
import { getUserProfile } from "@/modules/users/data-access"
import { getNotificationPreferences } from "@/modules/notifications/preferences"

export const dynamic = "force-dynamic"

export const metadata = {
  title: "Settings",
}

export default async function SettingsPage() {
  const ctx = await requireAuth()

  const userId = ctx.userId
  const userProfile = await getUserProfile(userId)

  if (!userProfile) redirect("/login")

  const roles = ctx.roles
  const notificationPrefs = await getNotificationPreferences(userId)

  if (roles.includes("admin")) {
    return <AdminSettingsView user={userProfile} notificationPreferences={notificationPrefs} />
  }
  if (roles.includes("student")) {
    return <StudentSettingsView user={userProfile} notificationPreferences={notificationPrefs} />
  }
  return <TeacherSettingsView user={userProfile} notificationPreferences={notificationPrefs} />
}
