import { notFound } from "next/navigation"
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 { getCoursePlanById } from "@/modules/course-plans/data-access"
import { CoursePlanDetail } from "@/modules/course-plans/components/course-plan-detail"

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

export const dynamic = "force-dynamic"

export default async function CoursePlanDetailPage({
  params,
}: {
  params: Promise<{ id: string }>
}): Promise<JSX.Element> {
  // P0-2：admin 页面补充 requirePermission 校验
  const ctx = await requirePermission(Permissions.COURSE_PLAN_READ)
  const { id } = await params
  // P0-1/P0-3：携带 scope 过滤，admin 视角为全局
  const plan = await getCoursePlanById(id, { userId: ctx.userId, isAdmin: true })

  if (!plan) notFound()

  return (
    <div className="flex h-full flex-col space-y-6 p-8">
      <CoursePlanDetail
        plan={plan}
        editHref={`/admin/course-plans/${plan.id}/edit`}
        backHref="/admin/course-plans"
        successHref="/admin/course-plans"
      />
    </div>
  )
}
