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 { GradesClient } from "@/modules/school/components/grades-view"
import { SchoolErrorBoundary } from "@/modules/school/components/school-error-boundary"
import { getGrades, getSchools, getStaffOptions, getGradeOverviewStats } from "@/modules/school/data-access"

export const dynamic = "force-dynamic"

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

export default async function AdminGradesPage(): Promise<JSX.Element> {
  await requirePermission(Permissions.SCHOOL_MANAGE)
  const t = await getTranslations("school")
  const [grades, schools, staff, gradeStats] = await Promise.all([
    getGrades(),
    getSchools(),
    getStaffOptions(),
    getGradeOverviewStats(),
  ])

  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("grades.title")}</h2>
        <p className="text-muted-foreground">{t("grades.description")}</p>
      </div>
      <SchoolErrorBoundary>
        <GradesClient grades={grades} schools={schools} staff={staff} gradeStats={gradeStats} />
      </SchoolErrorBoundary>
    </div>
  )
}
