import { notFound } from "next/navigation"
import type { Metadata } from "next"
import { getTranslations } from "next-intl/server"
import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { getMessageDetailPageData } from "@/modules/messaging/data-access"
import { MessageDetail } from "@/modules/messaging/components/message-detail"

export const dynamic = "force-dynamic"

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

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

  const message = await getMessageDetailPageData(id, ctx.userId)
  if (!message) notFound()

  return (
    <div className="flex h-full flex-col p-8">
      <MessageDetail message={message} currentUserId={ctx.userId} />
    </div>
  )
}
