import { notFound } from "next/navigation"
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 { getElectiveCourseById } from "@/modules/elective/data-access"
import { getCourseSelections } from "@/modules/elective/data-access-selections"
import { ElectiveCourseDetail } from "@/modules/elective/components/elective-course-detail"
import { ElectivePageLayout } from "@/modules/elective/components/elective-page-layout"

export const dynamic = "force-dynamic"

export default async function AdminElectiveDetailPage({
  params,
}: {
  params: Promise<{ id: string }>
}): Promise<JSX.Element> {
  const t = await getTranslations("elective")
  await requirePermission(Permissions.ELECTIVE_READ)
  const { id } = await params

  const [course, selections] = await Promise.all([
    getElectiveCourseById(id),
    getCourseSelections(id),
  ])

  if (!course) notFound()

  const header = (
    <div className="space-y-1">
      <h2 className="text-2xl font-bold tracking-tight">{t("title.detail")}</h2>
      <p className="text-muted-foreground">{t("description.detail")}</p>
    </div>
  )

  return (
    <ElectivePageLayout header={header}>
      <ElectiveCourseDetail
        course={course}
        selections={selections}
        editHref={`/admin/elective/${course.id}/edit`}
        backHref="/admin/elective"
      />
    </ElectivePageLayout>
  )
}
