import { getTranslations } from "next-intl/server"

import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { getMessagesPageData } from "@/modules/messaging/data-access"
import { MessageList } from "@/modules/messaging/components/message-list"
import { NotificationList } from "@/modules/notifications/components/notification-list"

export const dynamic = "force-dynamic"

export async function generateMetadata() {
  const t = await getTranslations("messages")
  return { title: t("title.list") }
}

export default async function MessagesPage() {
  const t = await getTranslations("messages")
  const ctx = await requirePermission(Permissions.MESSAGE_READ)

  const { messages: messagesResult, notifications: notificationsResult } =
    await getMessagesPageData(ctx.userId)

  return (
    <div className="flex h-full flex-col space-y-8 p-8">
      <div>
        <h2 className="text-2xl font-bold tracking-tight">{t("title.list")}</h2>
        <p className="text-muted-foreground">
          {t("description.list")}
        </p>
      </div>

      <MessageList messages={messagesResult.items} currentUserId={ctx.userId} />

      <NotificationList notifications={notificationsResult.items} />
    </div>
  )
}
