import React, { useState } from "react";
import { Head, Link } from "@inertiajs/react";

interface ErrorProps {
    status: number;
    message?: string;
}

export default function Error({ status, message }: ErrorProps) {
    const [copied, setCopied] = useState(false);
    const [showDetails, setShowDetails] = useState(false);

    const errorConfigs: Record<
        number,
        { code: string; title: string; description: string; color: string }
    > = {
        404: {
            code: "404",
            title: "Halaman Tidak Ditemukan",
            description:
                "Halaman atau data kunjungan yang Anda cari tidak ada, telah dihapus, atau URL yang dimasukkan salah.",
            color: "from-blue-500 to-indigo-600",
        },
        403: {
            code: "403",
            title: "Akses Ditolak",
            description:
                "Anda tidak memiliki izin atau hak akses yang cukup untuk membuka fitur halaman ini.",
            color: "from-amber-500 to-orange-600",
        },
        500: {
            code: "500",
            title: "Kesalahan Server",
            description:
                "Terjadi masalah pada sistem server kami. Tim teknis telah diberitahu mengenai masalah ini.",
            color: "from-rose-500 to-red-600",
        },
        503: {
            code: "503",
            title: "Sistem Sedang Pemeliharaan",
            description:
                "Layanan sedang ditingkatkan untuk sementara waktu. Silakan coba kembali beberapa saat lagi.",
            color: "from-emerald-500 to-teal-600",
        },
    };

    const config = errorConfigs[status] || {
        code: String(status),
        title: "Terjadi Kesalahan",
        description:
            "Sesuatu yang tidak terduga telah terjadi. Silakan kembali ke beranda.",
        color: "from-purple-500 to-violet-600",
    };

    const handleCopyLink = () => {
        const errorLog = `Error ${status}: ${config.title}\nURL: ${window.location.href}\nDetail: ${message || "N/A"}`;
        navigator.clipboard.writeText(errorLog);
        setCopied(true);
        setTimeout(() => setCopied(false), 2000);
    };

    return (
        <>
            <Head title={`${status} - ${config.title}`} />

            <div className="relative flex min-h-screen items-center justify-center overflow-hidden bg-slate-950 p-6 text-slate-100 font-sans antialiased">
                {/* Glow Effects Background */}
                <div
                    className={`absolute -top-40 -left-40 h-96 w-96 rounded-full bg-linear-to-br ${config.color} opacity-20 blur-3xl pointer-events-none`}
                />
                <div
                    className={`absolute -bottom-40 -right-40 h-96 w-96 rounded-full bg-linear-to-tl ${config.color} opacity-20 blur-3xl pointer-events-none`}
                />

                {/* Pattern Overlay */}
                <div
                    className="absolute inset-0 opacity-[0.03] pointer-events-none"
                    style={{
                        backgroundImage: `radial-gradient(circle at 1px 1px, #fff 1px, transparent 0)`,
                        backgroundSize: "24px 24px",
                    }}
                />

                {/* Error Card */}
                <div className="relative z-10 w-full max-w-lg rounded-3xl border border-slate-800 bg-slate-900/80 p-8 sm:p-10 shadow-2xl backdrop-blur-xl">
                    {/* Badge & Code */}
                    <div className="flex flex-col items-center text-center">
                        <div
                            className={`mb-4 inline-flex items-center gap-2 rounded-full border border-slate-700/50 bg-slate-800/60 px-4 py-1.5 text-xs font-semibold tracking-wide text-slate-300 shadow-inner`}
                        >
                            <span
                                className={`h-2 w-2 rounded-full bg-linear-to-r ${config.color} animate-pulse`}
                            />
                            Status HTTP {status}
                        </div>

                        <h1
                            className={`bg-linear-to-r ${config.color} bg-clip-text text-8xl font-black tracking-tight text-transparent drop-shadow-sm sm:text-9xl`}
                        >
                            {config.code}
                        </h1>

                        <h2 className="mt-4 text-2xl font-bold tracking-tight text-white sm:text-3xl">
                            {config.title}
                        </h2>

                        <p className="mt-3 text-sm leading-relaxed text-slate-400 sm:text-base">
                            {config.description}
                        </p>
                    </div>

                    {/* Collapsible Technical Details */}
                    {message && (
                        <div className="mt-6 rounded-2xl border border-slate-800 bg-slate-950/60 p-4">
                            <button
                                onClick={() => setShowDetails(!showDetails)}
                                className="flex w-full items-center justify-between text-xs font-medium text-slate-400 hover:text-slate-200 transition"
                            >
                                <span>Lihat Detail Keterangan Tech</span>
                                <span>{showDetails ? "▲" : "▼"}</span>
                            </button>
                            {showDetails && (
                                <div className="mt-3 border-t border-slate-800/80 pt-3 text-left">
                                    <pre className="max-h-32 overflow-x-auto rounded-lg bg-slate-900 p-2.5 text-[11px] font-mono text-rose-400 leading-relaxed">
                                        {message}
                                    </pre>
                                </div>
                            )}
                        </div>
                    )}

                    {/* Actions */}
                    <div className="mt-8 flex flex-col sm:flex-row items-center gap-3">
                        <Link
                            href="/dashboard"
                            className={`flex h-11 w-full sm:flex-1 items-center justify-center gap-2 rounded-xl bg-linear-to-r ${config.color} font-medium text-white shadow-lg transition duration-200 hover:opacity-90 active:scale-[0.98]`}
                        >
                            <svg
                                className="h-4 w-4"
                                fill="none"
                                viewBox="0 0 24 24"
                                stroke="currentColor"
                                strokeWidth={2}
                            >
                                <path
                                    strokeLinecap="round"
                                    strokeLinejoin="round"
                                    d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"
                                />
                            </svg>
                            Ke Beranda
                        </Link>

                        <button
                            onClick={handleCopyLink}
                            className="flex h-11 w-full sm:w-auto items-center justify-center gap-2 rounded-xl border border-slate-700 bg-slate-800/50 px-4 font-medium text-slate-300 transition hover:bg-slate-800 hover:text-white active:scale-[0.98]"
                            title="Salin rincian error untuk dilaporkan"
                        >
                            {copied ? (
                                <>
                                    <svg
                                        className="h-4 w-4 text-emerald-400"
                                        fill="none"
                                        viewBox="0 0 24 24"
                                        stroke="currentColor"
                                        strokeWidth={2}
                                    >
                                        <path
                                            strokeLinecap="round"
                                            strokeLinejoin="round"
                                            d="M5 13l4 4L19 7"
                                        />
                                    </svg>
                                    <span className="text-emerald-400 text-sm">
                                        Tersalin
                                    </span>
                                </>
                            ) : (
                                <>
                                    <svg
                                        className="h-4 w-4"
                                        fill="none"
                                        viewBox="0 0 24 24"
                                        stroke="currentColor"
                                        strokeWidth={2}
                                    >
                                        <path
                                            strokeLinecap="round"
                                            strokeLinejoin="round"
                                            d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"
                                        />
                                    </svg>
                                    <span className="text-sm">Salin Info</span>
                                </>
                            )}
                        </button>
                    </div>
                </div>
            </div>
        </>
    );
}
