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 { getSearchParam, type SearchParams } from "@/shared/lib/utils"
import { getAuditLogs, getAuditModuleOptions } from "@/modules/audit/data-access"
import { AuditLogView } from "@/modules/audit/components/audit-log-view"
import { AuditLogExportButton } from "@/modules/audit/components/audit-log-export-button"
import { AuditErrorBoundary } from "@/modules/audit/components/audit-error-boundary"
import type { AuditLogStatus } from "@/modules/audit/types"

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

export const dynamic = "force-dynamic"

const isValidAuditLogStatus = (v?: string): v is AuditLogStatus =>
  v === "success" || v === "failure"

export default async function AuditLogsPage({
  searchParams,
}: {
  searchParams: Promise<SearchParams>
}): Promise<JSX.Element> {
  const t = await getTranslations("audit")
  await requirePermission(Permissions.AUDIT_LOG_READ)

  const params = await searchParams
  const page = Number(getSearchParam(params, "page") ?? "1") || 1
  const moduleFilter = getSearchParam(params, "module") ?? undefined
  const action = getSearchParam(params, "action") ?? undefined
  const statusParam = getSearchParam(params, "status")
  const status = isValidAuditLogStatus(statusParam) ? statusParam : undefined
  const userId = getSearchParam(params, "userId") ?? undefined
  const startDate = getSearchParam(params, "startDate") ?? undefined
  const endDate = getSearchParam(params, "endDate") ?? undefined

  const [result, moduleOptions] = await Promise.all([
    getAuditLogs({ page, module: moduleFilter, action, status, userId, startDate, endDate }),
    getAuditModuleOptions(),
  ])

  const exportParams: Record<string, string> = {}
  if (moduleFilter) exportParams.module = moduleFilter
  if (action) exportParams.action = action
  if (status) exportParams.status = status
  if (userId) exportParams.userId = userId
  if (startDate) exportParams.startDate = startDate
  if (endDate) exportParams.endDate = endDate

  return (
    <div className="flex h-full flex-col space-y-8 p-8">
      <div className="flex items-start justify-between gap-4">
        <div className="space-y-1">
          <h2 className="text-2xl font-bold tracking-tight">{t("title")}</h2>
          <p className="text-muted-foreground">{t("description")}</p>
        </div>
        <AuditLogExportButton exportType="audit" params={exportParams} />
      </div>
      <AuditErrorBoundary>
        <AuditLogView
          items={result.items}
          page={result.page}
          pageSize={result.pageSize}
          total={result.total}
          totalPages={result.totalPages}
          moduleOptions={moduleOptions}
        />
      </AuditErrorBoundary>
    </div>
  )
}
