"use client";

import Link from "next/link";
import { AlertTriangle, Lock, ServerCrash, FileQuestion } from "lucide-react";
import type { ComponentType } from "react";
import { Button } from "@/components/ui/button";

export interface ErrorPageProps {
  code: number;
  title: string;
  message: string;
  actionLabel?: string;
  actionLink?: string;
  /** When provided, action renders as a button that calls this instead of linking */
  onActionClick?: () => void;
}

/**
 * Shared error screen for 403 / 500 / generic failures.
 *
 * Rebuilt on the design system. The previous version used a 10–15rem numeral, a
 * bouncing title, a hard-coded `#FFA626` accent that exists nowhere else in the
 * product, and drove its button hover from inline `onMouseEnter` handlers that
 * wrote `style.backgroundColor` directly.
 *
 * The status code is now supporting text rather than the loudest element — on
 * an error screen the sentence explaining what happened is what the reader
 * needs, not the number.
 */

const ICONS: Record<number, ComponentType<{ className?: string }>> = {
  403: Lock,
  404: FileQuestion,
  500: ServerCrash,
};

/** 5xx is a system fault (destructive); 4xx is expected (neutral/warning). */
const TONE: Record<number, string> = {
  403: "bg-warning-soft text-warning",
  404: "bg-muted text-muted-foreground",
  500: "bg-destructive-soft text-destructive",
};

export default function ErrorPage({
  code,
  title,
  message,
  actionLabel = "Go home",
  actionLink = "/",
  onActionClick,
}: ErrorPageProps) {
  const Icon = ICONS[code] ?? AlertTriangle;
  const tone = TONE[code] ?? "bg-muted text-muted-foreground";

  return (
    <main className="flex min-h-screen items-center justify-center bg-background px-4 py-16">
      <div className="w-full max-w-md text-center">
        <span
          className={`mx-auto mb-5 flex size-12 items-center justify-center rounded-full ${tone}`}
          aria-hidden
        >
          <Icon className="size-6" />
        </span>

        <p className="oxy-eyebrow">Error {code}</p>
        <h1 className="mt-1.5 text-2xl font-semibold tracking-tight text-foreground">
          {title}
        </h1>
        <p className="mx-auto mt-2 max-w-sm text-sm leading-relaxed text-muted-foreground">
          {message}
        </p>

        <div className="mt-6">
          {onActionClick ? (
            <Button onClick={onActionClick} className="w-full sm:w-auto">
              {actionLabel}
            </Button>
          ) : (
            <Button asChild className="w-full sm:w-auto">
              <Link href={actionLink}>{actionLabel}</Link>
            </Button>
          )}
        </div>
      </div>
    </main>
  );
}
