"use client";

import { useEffect } from "react";
import { Button } from "@/components/ui/button";
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from "@/components/ui/card";

export default function AdminError({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  useEffect(() => {
    console.error("Admin error:", error);
  }, [error]);

  return (
    <div className="max-w-2xl mx-auto p-6">
      <Card className="border-red-500/30">
        <CardHeader>
          <CardTitle className="text-red-300">Admin panel crashed</CardTitle>
          <CardDescription>
            A component threw an error. Other tabs may still work.
          </CardDescription>
        </CardHeader>
        <CardContent className="space-y-3">
          <pre className="rounded-md bg-muted p-3 text-xs text-muted-foreground whitespace-pre-wrap break-words max-h-48 overflow-auto">
            {error.message}
            {error.digest ? `\n\nref: ${error.digest}` : ""}
          </pre>
          <div className="flex gap-2">
            <Button onClick={reset}>Retry</Button>
            <Button
              variant="ghost"
              onClick={() => {
                if (typeof window !== "undefined") window.location.reload();
              }}
            >
              Reload page
            </Button>
          </div>
        </CardContent>
      </Card>
    </div>
  );
}
