import type { JSX } from "react"
import { getTeacherClasses } from "@/modules/classes/data-access"
import { getClassStudentsForAttendance } from "@/modules/attendance/data-access"
import { AttendanceSheet } from "@/modules/attendance/components/attendance-sheet"
import { getParam, type SearchParams } from "@/shared/lib/search-params"

export const dynamic = "force-dynamic"

export default async function AttendanceSheetPage({
  searchParams,
}: {
  searchParams: Promise<SearchParams>
}): Promise<JSX.Element> {
  const sp = await searchParams

  const defaultClassId = getParam(sp, "classId")
  const defaultDate = getParam(sp, "date")

  const [classes, students] = await Promise.all([
    getTeacherClasses(),
    defaultClassId
      ? getClassStudentsForAttendance(defaultClassId)
      : Promise.resolve([] as Awaited<ReturnType<typeof getClassStudentsForAttendance>>),
  ])
  const classOptions = classes.map((c) => ({ id: c.id, name: c.name }))

  return (
    <div className="h-full flex-1 flex-col space-y-8 p-8 md:flex">
      <div>
        <h1 className="text-2xl font-bold tracking-tight">Take Attendance</h1>
        <p className="text-muted-foreground">
          Select a class and date, then mark attendance for each student.
        </p>
      </div>

      <AttendanceSheet
        classes={classOptions}
        students={students}
        defaultClassId={defaultClassId}
        defaultDate={defaultDate}
      />
    </div>
  )
}
