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 {
  getAuditOverviewStats,
  getAuditTrend,
  getDataChangeActionStats,
} from "@/modules/audit/data-access"
import { AuditOverviewView } from "@/modules/audit/components/audit-overview-view"
import { AuditServiceProvider } from "@/modules/audit/services/audit-service"
import { adminAuditService } from "@/modules/audit/services/admin-audit-service"

export async function generateMetadata(): Promise<Metadata> {
  const t = await getTranslations("audit.overview")
  return {
    title: `${t("title")} - Next_Edu`,
    description: t("description"),
  }
}

export const dynamic = "force-dynamic"

export default async function AuditOverviewPage(): Promise<JSX.Element> {
  await requirePermission(Permissions.AUDIT_LOG_READ)

  const [stats, trend, distribution] = await Promise.all([
    getAuditOverviewStats(),
    getAuditTrend(7),
    getDataChangeActionStats(),
  ])

  return (
    <AuditServiceProvider service={adminAuditService}>
      <AuditOverviewView stats={stats} trend={trend} distribution={distribution} />
    </AuditServiceProvider>
  )
}
