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 } from "@/modules/classes/data-access"
import { getAcademicYears, getStaffOptions, getSubjectOptions } from "@/modules/school/data-access"
import { CoursePlanForm } from "@/modules/course-plans/components/course-plan-form"

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

export const dynamic = "force-dynamic"

export default async function CreateCoursePlanPage(): Promise<JSX.Element> {
  const t = await getTranslations("coursePlans")
  // P0-2：admin 页面补充 requirePermission 校验
  await requirePermission(Permissions.COURSE_PLAN_MANAGE)

  const [classes, subjects, teachers, academicYears] = await Promise.all([
    getAdminClasses(),
    getSubjectOptions(),
    getStaffOptions(),
    getAcademicYears(),
  ])

  return (
    <div className="flex h-full flex-col space-y-8 p-8">
      <div>
        <h2 className="text-2xl font-bold tracking-tight">{t("create.title")}</h2>
        <p className="text-muted-foreground">{t("create.description")}</p>
      </div>
      <CoursePlanForm
        mode="create"
        classes={classes.map((c) => ({ id: c.id, name: c.name }))}
        subjects={subjects}
        teachers={teachers.map((tch) => ({ id: tch.id, name: tch.name }))}
        academicYears={academicYears.map((y) => ({ id: y.id, name: y.name }))}
        backHref="/admin/course-plans"
        successHref="/admin/course-plans"
      />
    </div>
  )
}
