"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { useEffect } from "react";
import { isLocale, type Locale } from "@/lib/i18n/config";
import { getDictionary } from "@/lib/i18n";

export default function StoreError({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  const pathname = usePathname();
  const segment = pathname?.split("/")[1] ?? "fr";
  const locale: Locale = isLocale(segment) ? segment : "fr";
  const dict = getDictionary(locale);

  useEffect(() => {
    console.error(error);
  }, [error]);

  return (
    <section className="shell flex min-h-[65vh] flex-col items-center justify-center py-20 text-center">
      <p className="eyebrow">{dict.errors.generic}</p>
      <h1 className="mt-4 max-w-xl font-serif text-3xl font-light leading-tight md:text-5xl">
        {dict.errors.generic}
      </h1>
      {error.digest && <p className="mt-2 font-mono text-[12px] text-charcoal/30">{error.digest}</p>}
      <div className="mt-8 flex flex-wrap justify-center gap-3">
        <button
          type="button"
          onClick={reset}
          className="h-11 bg-charcoal px-6 text-[12px] uppercase tracking-[0.16em] text-ivory hover:opacity-90"
        >
          {dict.common.apply}
        </button>
        <Link
          href={`/${locale}`}
          className="flex h-11 items-center border border-charcoal/20 px-6 text-[12px] uppercase tracking-[0.16em] text-charcoal hover:border-charcoal"
        >
          {dict.errors.backHome}
        </Link>
      </div>
    </section>
  );
}
