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 { getAdminAnnouncementDetailPageData } from "@/modules/announcements/data-access"
import { AnnouncementDetail } from "@/modules/announcements/components/announcement-detail"
import { AnnouncementsServiceProvider } from "@/modules/announcements/components/announcements-service-context"

export const dynamic = "force-dynamic"

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

/**
 * P1-2: 新增管理端公告详情页。
 *
 * 此前 `AnnouncementDetail canManage=true` 分支为死代码（无任何页面使用），
 * 管理员只能通过编辑表单间接查看公告，无法在 UI 中执行发布/归档/删除/置顶。
 * 本页面启用该分支，提供完整的管理操作入口。
 */
export default async function AdminAnnouncementDetailPage({
  params,
}: {
  params: Promise<{ id: string }>
}): Promise<JSX.Element> {
  await requirePermission(Permissions.ANNOUNCEMENT_MANAGE)
  const { id } = await params

  const { announcement } = await getAdminAnnouncementDetailPageData(id)
  if (!announcement) notFound()

  return (
    <AnnouncementsServiceProvider>
      <div className="flex h-full flex-col space-y-8 p-8">
        <AnnouncementDetail
          announcement={announcement}
          canManage
          editHref={`/admin/announcements/${id}/edit`}
          backHref="/admin/announcements"
        />
      </div>
    </AnnouncementsServiceProvider>
  )
}
