"use client";

import { useEffect } from "react";
import { AlertTriangle, RefreshCw, Home } from "lucide-react";
import Link from "next/link";

export default function ContentBriefError({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  useEffect(() => {
    // Log to the browser console so devs have the full stack while
    // the user sees the friendly fallback. Server-side logging picks
    // it up via Next.js's built-in error reporter.
    console.error("[content-brief] crash:", error);
  }, [error]);

  return (
    <div className="max-w-2xl mx-auto px-6 py-16">
      <div className="bg-white rounded-xl border border-red-200 p-8 text-center space-y-4">
        <AlertTriangle className="w-10 h-10 text-red-500 mx-auto" />
        <div>
          <h2 className="text-lg font-bold text-slate-900 mb-1">
            Something went wrong loading the content brief
          </h2>
          <p className="text-sm text-slate-500">
            The page hit an unexpected error. Try reloading — if the problem
            persists, the keyword or brand data may need a refresh.
          </p>
        </div>
        <details className="text-left bg-slate-50 border border-slate-200 rounded-lg p-3 text-xs">
          <summary className="font-semibold text-slate-600 cursor-pointer">Error details</summary>
          <p className="mt-2 font-mono text-red-600 break-all">{error.message || "Unknown error"}</p>
          {error.digest && (
            <p className="mt-1 text-slate-400">Digest: {error.digest}</p>
          )}
        </details>
        <div className="flex items-center justify-center gap-2">
          <button
            onClick={reset}
            className="inline-flex items-center gap-1.5 px-4 py-2 bg-brand-600 text-white text-sm font-semibold rounded-lg hover:bg-brand-700"
          >
            <RefreshCw className="w-4 h-4" /> Try again
          </button>
          <Link
            href="/dashboard"
            className="inline-flex items-center gap-1.5 px-4 py-2 bg-white text-slate-700 border border-slate-200 text-sm font-semibold rounded-lg hover:bg-slate-50"
          >
            <Home className="w-4 h-4" /> Go to dashboard
          </Link>
        </div>
      </div>
    </div>
  );
}
