"use client"

/**
 * 学校模块 Error Boundary。
 *
 * 薄包装：委托给共享 SectionErrorBoundary，通过自定义 fallback 实现
 * 重试时调用 router.refresh() 刷新服务端数据。
 * 保留同名导出以兼容现有 import。
 */

import type { ReactNode } from "react"
import { AlertCircle } from "lucide-react"
import { useRouter } from "next/navigation"
import { useTranslations } from "next-intl"
import { Button } from "@/shared/components/ui/button"
import { SectionErrorBoundary } from "@/shared/components/section-error-boundary"

interface SchoolErrorBoundaryProps {
  children: ReactNode
  fallback?: ReactNode
}

export function SchoolErrorBoundary({
  children,
  fallback,
}: SchoolErrorBoundaryProps): ReactNode {
  const t = useTranslations("school")
  const router = useRouter()

  const customFallback = (_error: Error, reset: () => void): ReactNode => {
    const handleRetry = (): void => {
      reset()
      router.refresh()
    }
    return (
      <div
        role="alert"
        className="flex min-h-[400px] flex-col items-center justify-center rounded-md border border-dashed p-8 text-center"
      >
        <div className="flex h-16 w-16 items-center justify-center rounded-full bg-destructive/10">
          <AlertCircle className="h-8 w-8 text-destructive" aria-hidden="true" />
        </div>
        <h3 className="mt-4 text-lg font-semibold">{t("errors.boundary.title")}</h3>
        <p className="mb-4 mt-2 max-w-md text-sm text-muted-foreground">
          {t("errors.boundary.description")}
        </p>
        <Button onClick={handleRetry}>{t("errors.boundary.retry")}</Button>
      </div>
    )
  }

  return (
    <SectionErrorBoundary fallback={fallback ? () => fallback : customFallback}>
      {children}
    </SectionErrorBoundary>
  )
}
