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 { listInvitationCodes } from "@/modules/invitation-codes/data-access"
import { InvitationCodesView } from "@/modules/invitation-codes/components/invitation-codes-view"

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

export const dynamic = "force-dynamic"

export default async function AdminInvitationCodesPage(): Promise<JSX.Element> {
  await requirePermission(Permissions.USER_MANAGE)

  // 直接调用 data-access 获取初始数据（SSR）
  // 后续生成/删除通过 Server Action + router.refresh() 触发更新
  const codes = await listInvitationCodes(200, true)
  // 在 Server Component 中计算「当前时间」传给 Client Component，
  // 避免 Client 端在 render 阶段调用 Date.now()（react-hooks/purity 规则）。
  // 此处使用 new Date().getTime() 与现有 student/learning/page.tsx 模式一致。
  const now = new Date().getTime()

  return <InvitationCodesView initialCodes={codes} now={now} />
}
