import type { Metadata } from "next"
import type { JSX } from "react"
import { UserX } from "lucide-react"
import { getTranslations } from "next-intl/server"

import { AiStudyPath } from "@/modules/ai/components/ai-study-path"
import { getCurrentStudentUser } from "@/modules/users/data-access"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"

export const dynamic = "force-dynamic"

export async function generateMetadata(): Promise<Metadata> {
  const t = await getTranslations("student.studyPath")
  return {
    title: t("title"),
    description: t("description"),
  }
}

export default async function StudentStudyPathPage(): Promise<JSX.Element> {
  const t = await getTranslations("student.studyPath")
  // P0 显式权限校验：与 AI_CHAT 一致，防止 student 角色被关闭后页面仍可访问
  await requirePermission(Permissions.AI_CHAT)
  const student = await getCurrentStudentUser()

  if (!student) {
    return (
      <div className="space-y-8">
        <EmptyState
          title={t("noUser")}
          description={t("noUserDesc")}
          icon={UserX}
        />
      </div>
    )
  }

  return (
    <div className="space-y-8">
      <div>
        <h2 className="text-2xl font-bold tracking-tight">{t("title")}</h2>
        <p className="text-muted-foreground">{t("description")}</p>
      </div>
      {/* AiStudyPath 是客户端组件，AiClientProvider 由 dashboard layout 注入 */}
      <AiStudyPath studentId={student.id} />
    </div>
  )
}
