import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' import { redirect } from 'next/navigation' import { db } from '@/lib/db' import { teacherAssignments, sections, classes, periods, studentEnrollments, attendance, eq, and } from '@/lib/db/schema' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { BookOpen, Users, ClipboardCheck, Calendar } from 'lucide-react' import { DashboardLayout } from '@/components/dashboard-layout' interface TeacherStats { totalSections: number totalStudents: number attendanceRecords: number activePeriods: number } interface AssignedSection { id: string name: string className: string periodName: string studentCount: number maxStudents: number } async function getTeacherStats(teacherId: string): Promise { // Get teacher's assigned sections const assignedSections = await db .select({ sectionId: teacherAssignments.sectionId }) .from(teacherAssignments) .where(eq(teacherAssignments.teacherId, teacherId)) const sectionIds = assignedSections.map(a => a.sectionId) if (sectionIds.length === 0) { return { totalSections: 0, totalStudents: 0, attendanceRecords: 0, activePeriods: 0 } } // Count total students enrolled in teacher's sections const studentCount = await db .select() .from(studentEnrollments) .where(eq(studentEnrollments.sectionId, sectionIds[0] as string)) // This is a simplified query // Count attendance records for teacher's sections const attendanceCount = await db .select() .from(attendance) .where(eq(attendance.sectionId, sectionIds[0] as string)) // This is a simplified query // Count active periods const activePeriods = await db .select() .from(periods) .where(eq(periods.isActive, true)) return { totalSections: sectionIds.length, totalStudents: studentCount.length, attendanceRecords: attendanceCount.length, activePeriods: activePeriods.length } } async function getAssignedSections(teacherId: string): Promise { const result = await db .select({ sectionId: sections.id, sectionName: sections.name, className: classes.name, periodName: periods.name, maxStudents: sections.maxStudents }) .from(teacherAssignments) .innerJoin(sections, eq(teacherAssignments.sectionId, sections.id)) .innerJoin(classes, eq(sections.classId, classes.id)) .innerJoin(periods, eq(classes.periodId, periods.id)) .where(eq(teacherAssignments.teacherId, teacherId)) // Get student count for each section const sectionsWithStudents = await Promise.all( result.map(async (section) => { const studentCount = await db .select() .from(studentEnrollments) .where(eq(studentEnrollments.sectionId, section.sectionId)) return { id: section.sectionId, name: section.sectionName, className: section.className, periodName: section.periodName, studentCount: studentCount.length, maxStudents: section.maxStudents || 0 } }) ) return sectionsWithStudents } export default async function TeacherDashboard() { const session = await getServerSession(authOptions) if (!session || session.user.role !== 'teacher') { redirect('/auth/signin') } const stats = await getTeacherStats(session.user.id) const assignedSections = await getAssignedSections(session.user.id) const breadcrumbs = [ { label: "Dashboard" } ] return (

Bienvenido, {session.user.firstName} {session.user.lastName}

Gestiona tus clases y estudiantes desde aquí

{/* Stats Cards */}
Secciones Asignadas
{stats.totalSections}
Total Estudiantes
{stats.totalStudents}
Registros de Asistencia
{stats.attendanceRecords}
Períodos Activos
{stats.activePeriods}
{/* Assigned Sections */} Mis Secciones {assignedSections.length === 0 ? (

No tienes secciones asignadas actualmente.

) : (
{assignedSections.map((section) => (

{section.className} - {section.name}

Período: {section.periodName}

{section.studentCount}/{section.maxStudents} estudiantes

))}
)}
) }