"use client";

import Link from "next/link";

/**
 * Route-level error boundary for the Site Finder analysis page (#1001, 958-B5).
 *
 * Net of last resort: even if an unforeseen payload (e.g. a 202 stub missing
 * `score`/`competitors`) slips past the query/section guards and throws during
 * render, the App Router catches it here and shows a graceful card instead of
 * a blank white screen.
 */
export default function Error({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  return (
    <main style={{ padding: "24px 20px", maxWidth: 1400, margin: "0 auto" }}>
      <div
        style={{
          marginTop: 24,
          background: "var(--bg-card, #FFFFFF)",
          border: "1px solid var(--border-card, #E6E8EC)",
          borderRadius: 12,
          padding: "24px",
        }}
      >
        <h1
          style={{
            margin: 0,
            fontSize: 18,
            fontWeight: 600,
            color: "var(--fg-primary, #111111)",
          }}
        >
          Не удалось отобразить анализ участка
        </h1>
        <p
          style={{
            margin: "12px 0 0",
            fontSize: 13,
            color: "var(--fg-secondary, #5B6066)",
          }}
        >
          {process.env.NODE_ENV === "production"
            ? "Попробуйте обновить страницу или вернуться к выбору участка."
            : error.message || "Произошла непредвиденная ошибка при отрисовке."}
        </p>
        <div style={{ display: "flex", gap: 12, marginTop: 24 }}>
          <button
            type="button"
            onClick={reset}
            style={{
              padding: "8px 18px",
              fontSize: 14,
              fontWeight: 600,
              background: "var(--accent, #1D4ED8)",
              color: "var(--fg-on-dark, #E2E8F0)",
              border: "none",
              borderRadius: 8,
              cursor: "pointer",
            }}
          >
            Повторить
          </button>
          <Link
            href="/site-finder"
            style={{
              display: "inline-flex",
              alignItems: "center",
              padding: "8px 18px",
              fontSize: 14,
              fontWeight: 500,
              color: "var(--accent, #1D4ED8)",
              textDecoration: "none",
            }}
          >
            ← Site Finder
          </Link>
        </div>
      </div>
    </main>
  );
}
