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 { getAdminClasses, getTeacherOptions } from "@/modules/classes/data-access"
import { ClassErrorBoundary } from "@/modules/classes/components/class-error-boundary"
import { AdminClassesClient } from "@/modules/classes/components/admin-classes-view"
import { getGrades, getSchools } from "@/modules/school/data-access"

export const dynamic = "force-dynamic"

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

export default async function AdminSchoolClassesPage(): Promise<JSX.Element> {
  await requirePermission(Permissions.SCHOOL_MANAGE)
  const t = await getTranslations("school")
  const [classes, teachers, schools, grades] = await Promise.all([
    getAdminClasses(),
    getTeacherOptions(),
    getSchools(),
    getGrades(),
  ])

  return (
    <div className="flex h-full flex-col space-y-8 p-8">
      <div className="space-y-1">
        <h2 className="text-2xl font-bold tracking-tight">{t("classManagement.title")}</h2>
        <p className="text-muted-foreground">{t("classManagement.description")}</p>
      </div>
      <ClassErrorBoundary>
        <AdminClassesClient classes={classes} teachers={teachers} schools={schools} grades={grades} />
      </ClassErrorBoundary>
    </div>
  )
}
