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 { getCoursePlans } from "@/modules/course-plans/data-access"
import { CoursePlanList } from "@/modules/course-plans/components/course-plan-list"

export const dynamic = "force-dynamic"

export default async function StudentCoursePlansPage(): Promise<JSX.Element> {
  const ctx = await requirePermission(Permissions.COURSE_PLAN_READ)
  const t = await getTranslations("coursePlans")

  // 学生视角：通过 class_members scope 获取 classIds
  let classIds: string[] = []
  if (ctx.dataScope.type === "class_members") {
    classIds = ctx.dataScope.classIds
  }

  const plans =
    classIds.length > 0
      ? await getCoursePlans(
          { status: "active" },
          { userId: ctx.userId, isAdmin: false, classIds }
        )
      : []

  return (
    <div className="flex h-full flex-col space-y-8 p-8">
      <div className="space-y-1">
        <h1 className="text-2xl font-bold tracking-tight">{t("student.title")}</h1>
        <p className="text-muted-foreground">{t("student.description")}</p>
      </div>
      <CoursePlanList
        plans={plans}
        detailBaseHref="/student/course-plans"
      />
    </div>
  )
}
