import Link from "next/link" import { redirect } from "next/navigation" import { auth } from "@/auth" import { getStudentClasses, getStudentSchedule, getTeacherClasses, getTeacherTeachingSubjects } from "@/modules/classes/data-access" import { StudentGradesCard } from "@/modules/dashboard/components/student-dashboard/student-grades-card" import { StudentStatsGrid } from "@/modules/dashboard/components/student-dashboard/student-stats-grid" import { StudentTodayScheduleCard } from "@/modules/dashboard/components/student-dashboard/student-today-schedule-card" import { StudentUpcomingAssignmentsCard } from "@/modules/dashboard/components/student-dashboard/student-upcoming-assignments-card" import { getStudentDashboardGrades, getStudentHomeworkAssignments } from "@/modules/homework/data-access" import { getUserProfile } from "@/modules/users/data-access" import { Permissions } from "@/shared/types/permissions" import { Badge } from "@/shared/components/ui/badge" import { Button } from "@/shared/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/shared/components/ui/card" import { Separator } from "@/shared/components/ui/separator" import { User, Mail, Phone, MapPin, Calendar, Clock, Shield } from "lucide-react" export const dynamic = "force-dynamic" const toWeekday = (d: Date): 1 | 2 | 3 | 4 | 5 | 6 | 7 => { const day = d.getDay() return (day === 0 ? 7 : day) as 1 | 2 | 3 | 4 | 5 | 6 | 7 } const formatDate = (date: Date | null) => { if (!date) return "-" return new Intl.DateTimeFormat("en-US", { year: "numeric", month: "long", day: "numeric", }).format(date) } export default async function ProfilePage() { const session = await auth() if (!session?.user) redirect("/login") const userId = String(session.user.id ?? "").trim() const userProfile = await getUserProfile(userId) if (!userProfile) { redirect("/login") } const permissions = session.user.permissions ?? [] const isStudent = permissions.includes(Permissions.HOMEWORK_SUBMIT) && !permissions.includes(Permissions.EXAM_CREATE) const isTeacher = permissions.includes(Permissions.EXAM_CREATE) const studentData = isStudent ? await (async () => { const [classes, schedule, assignmentsAll, grades] = await Promise.all([ getStudentClasses(userId), getStudentSchedule(userId), getStudentHomeworkAssignments(userId), getStudentDashboardGrades(userId), ]) const now = new Date() const in7Days = new Date(now) in7Days.setDate(in7Days.getDate() + 7) const dueSoonCount = assignmentsAll.filter((a) => { if (!a.dueAt) return false const due = new Date(a.dueAt) return due >= now && due <= in7Days && a.progressStatus !== "graded" }).length const overdueCount = assignmentsAll.filter((a) => { if (!a.dueAt) return false const due = new Date(a.dueAt) return due < now && a.progressStatus !== "graded" }).length const gradedCount = assignmentsAll.filter((a) => a.progressStatus === "graded").length const upcomingAssignments = [...assignmentsAll] .sort((a, b) => { const aTime = a.dueAt ? new Date(a.dueAt).getTime() : Number.POSITIVE_INFINITY const bTime = b.dueAt ? new Date(b.dueAt).getTime() : Number.POSITIVE_INFINITY if (aTime !== bTime) return aTime - bTime return a.id.localeCompare(b.id) }) .slice(0, 8) const todayWeekday = toWeekday(now) const todayScheduleItems = schedule .filter((s) => s.weekday === todayWeekday) .map((s) => ({ id: s.id, classId: s.classId, className: s.className, course: s.course, startTime: s.startTime, endTime: s.endTime, location: s.location ?? null, })) return { enrolledClassCount: classes.length, dueSoonCount, overdueCount, gradedCount, todayScheduleItems, upcomingAssignments, grades, } })() : null const teacherData = isTeacher ? await (async () => { const [subjects, classes] = await Promise.all([getTeacherTeachingSubjects(), getTeacherClasses()]) return { subjects, classes } })() : null return (

Profile

Manage your personal and account information.
Personal Information Basic personal details.
Full Name
{userProfile.name ?? "-"}
Gender
{userProfile.gender ?? "-"}
Age
{userProfile.age ?? "-"}
Phone
{userProfile.phone ? : null} {userProfile.phone ?? "-"}
Address
{userProfile.address ? : null} {userProfile.address ?? "-"}
Account Information System account details.
Email
{userProfile.email}
Role
{userProfile.role}
Member Since
{formatDate(userProfile.createdAt)}
Onboarded At
{formatDate(userProfile.onboardedAt)}
{studentData ? (

Student Overview

Your academic performance and schedule.
) : null} {teacherData ? (

Teacher Overview

Your teaching subjects and classes.
Teaching Subjects Subjects you are currently assigned to teach. {teacherData.subjects.length === 0 ? (
No subjects assigned yet.
) : (
{teacherData.subjects.map((subject) => ( {subject} ))}
)}
Teaching Classes Classes you are currently managing. {teacherData.classes.length === 0 ? (
No classes assigned yet.
) : ( teacherData.classes.map((cls) => (
{cls.name}
{cls.grade} {cls.homeroom ? ` • ${cls.homeroom}` : ""}
)) )}
) : null}
) }