import type { Metadata } from "next"
import type { JSX } from "react"

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 type { AuditLogStatus } from "@/modules/audit/types"

export const metadata: Metadata = {
  title: "审计日志 - Next_Edu",
  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> {
  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 startDate = getSearchParam(params, "startDate") ?? undefined
  const endDate = getSearchParam(params, "endDate") ?? undefined

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

  const exportParams: Record<string, string> = {}
  if (moduleFilter) exportParams.module = moduleFilter
  if (action) exportParams.action = action
  if (status) exportParams.status = status
  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">审计日志</h2>
          <p className="text-muted-foreground">
            追踪系统内所有用户操作，保障安全与合规。
          </p>
        </div>
        <AuditLogExportButton exportType="audit" params={exportParams} />
      </div>
      <AuditLogView
        items={result.items}
        page={result.page}
        pageSize={result.pageSize}
        total={result.total}
        totalPages={result.totalPages}
        moduleOptions={moduleOptions}
      />
    </div>
  )
}
