"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import DragonEye from "@/components/DragonEye";

export default function LoginPage() {
    const [password, setPassword] = useState("");
    const [error, setError] = useState(false);
    const [loading, setLoading] = useState(false);
    const router = useRouter();

    const handleLogin = async (e: React.FormEvent) => {
        e.preventDefault();
        setLoading(true);
        setError(false);
        await new Promise((r) => setTimeout(r, 500));

        if (password === process.env.NEXT_PUBLIC_PASSWORD) {
            sessionStorage.setItem("wyview_auth", "true");
            router.push("/dashboard");
        } else {
            setError(true);
            setLoading(false);
        }
    };

    return (
        <main className="min-h-screen bg-[#0a0d0f] flex flex-col items-center justify-center relative overflow-hidden">
            <div className="absolute inset-0 opacity-20 pointer-events-none" style={{
                backgroundImage: `linear-gradient(rgba(74,255,140,0.05) 1px, transparent 1px), linear-gradient(90deg, rgba(74,255,140,0.05) 1px, transparent 1px)`,
                backgroundSize: "40px 40px",
            }} />

            <div className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[500px] h-[500px] rounded-full bg-[#4aff8c]/[0.02] blur-3xl pointer-events-none" />

            <div className="relative z-10 flex flex-col items-center gap-8 w-full max-w-sm px-6">
                <div className="flex flex-col items-center gap-5">
                    <DragonEye size={80} />
                    <div className="text-center">
                        <div className="text-[9px] tracking-[0.45em] text-[#4aff8c]/50 uppercase mb-2 font-mono">
                            Analytics Dashboard
                        </div>
                        <h1 className="text-4xl font-black tracking-[0.15em] uppercase text-white">
                            WYVIEW
                        </h1>
                    </div>
                </div>

                <form onSubmit={handleLogin} className="w-full flex flex-col gap-3">
                    <input
                        type="password"
                        placeholder="Mot de passe"
                        value={password}
                        onChange={(e) => { setPassword(e.target.value); setError(false); }}
                        className={`w-full bg-[#0d1210] border px-4 py-3 text-sm font-mono text-white placeholder-[#2a3a2a] outline-none rounded-sm transition-all duration-200 ${
                            error ? "border-red-500/50" : "border-[#1a2a1a] focus:border-[#4aff8c]/40"
                        }`}
                    />
                    {error && (
                        <p className="text-red-400/80 text-[11px] font-mono tracking-widest">— ACCÈS REFUSÉ</p>
                    )}
                    <button
                        type="submit"
                        disabled={loading || !password}
                        className="w-full py-3 text-[11px] font-mono tracking-[0.3em] uppercase font-bold border border-[#4aff8c]/30 text-[#4aff8c] hover:bg-[#4aff8c]/8 hover:border-[#4aff8c]/60 transition-all duration-200 rounded-sm disabled:opacity-30 disabled:cursor-not-allowed"
                    >
                        {loading ? "VÉRIFICATION..." : "ACCÉDER"}
                    </button>
                </form>

                <div className="text-[9px] font-mono text-[#1a3a1a] tracking-[0.3em]">
                    WYVIEW v1.0 /// CROWMATE
                </div>
            </div>
        </main>
    );
}