import type { Metadata } from "next"
import type { JSX } from "react"

import { getTranslations } from "next-intl/server"
import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { getGradeManagedClasses, getManagedGrades, getTeacherOptions } from "@/modules/classes/data-access"
import { GradeClassesClient } from "@/modules/classes/components/grade-classes-view"

export const dynamic = "force-dynamic"

export async function generateMetadata(): Promise<Metadata> {
  const t = await getTranslations("school")
  return {
    title: `${t("classManagement.grade.title")} - Next_Edu`,
    description: t("classManagement.grade.description"),
  }
}

export default async function GradeClassesPage(): Promise<JSX.Element> {
  const ctx = await requirePermission(Permissions.GRADE_MANAGE)
  const t = await getTranslations("school")
  const userId = ctx.userId

  const [classes, teachers, managedGrades] = await Promise.all([
    getGradeManagedClasses(userId),
    getTeacherOptions(),
    getManagedGrades(userId),
  ])

  return (
    <div className="flex h-full flex-col space-y-8 p-8">
      <div className="flex flex-col justify-between space-y-4 md:flex-row md:items-center md:space-y-0">
        <div>
          <h2 className="text-2xl font-bold tracking-tight">{t("classManagement.grade.title")}</h2>
          <p className="text-muted-foreground">{t("classManagement.grade.description")}</p>
        </div>
      </div>

      <GradeClassesClient classes={classes} teachers={teachers} managedGrades={managedGrades} />
    </div>
  )
}
