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 { getEditAnnouncementPageData } from "@/modules/announcements/data-access"
import { AnnouncementForm } from "@/modules/announcements/components/announcement-form"
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.edit"),
    description: t("description.edit"),
  }
}

export default async function EditAnnouncementPage({
  params,
}: {
  params: Promise<{ id: string }>
}): Promise<JSX.Element> {
  await requirePermission(Permissions.ANNOUNCEMENT_MANAGE)
  const { id } = await params
  const t = await getTranslations("announcements")

  const { announcement, grades } = await getEditAnnouncementPageData(id)

  if (!announcement) notFound()

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