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 { getAnnouncementById, isAnnouncementReadByUser } from "@/modules/announcements/data-access"
import { AnnouncementDetail } from "@/modules/announcements/components/announcement-detail"

export const dynamic = "force-dynamic"

export async function generateMetadata(): Promise<Metadata> {
  const t = await getTranslations("announcements")
  return { title: t("title.detail") }
}

export default async function AnnouncementDetailPage({
  params,
}: {
  params: Promise<{ id: string }>
}): Promise<JSX.Element> {
  const { id } = await params
  const ctx = await requirePermission(Permissions.ANNOUNCEMENT_READ)

  const announcement = await getAnnouncementById(id)
  if (!announcement) notFound()

  // 获取当前用户的已读状态
  const isReadByCurrentUser = await isAnnouncementReadByUser(id, ctx.userId)

  return (
    <div className="flex h-full flex-col space-y-8 p-8">
      <AnnouncementDetail
        announcement={{ ...announcement, isReadByCurrentUser }}
        canManage={false}
        backHref="/announcements"
      />
    </div>
  )
}
