import { useState } from "react";
import { useNavigate } from "react-router";
import { LogIn, Loader2, Eye, EyeOff } from "lucide-react";
import { login } from "../../services/api";
import logoUrl from "../../assets/maintiva-logo.jpg";

export default function Login() {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState("");
  const [isLoading, setIsLoading] = useState(false);
  const [showPassword, setShowPassword] = useState(false);
  const navigate = useNavigate();

  const handleLogin = async (e: React.FormEvent) => {
    e.preventDefault();
    setError("");

    if (!email || !password) {
      setError("Please enter both email and password");
      return;
    }

    if (!/\S+@\S+\.\S+/.test(email)) {
      setError("Please enter a valid email address");
      return;
    }

    setIsLoading(true);
    try {
      const res = await login(email, password);
      const user = {
        user_id: res.data.id,
        email: res.data.email,
        name: res.data.fullname,
        loginTime: new Date().toISOString(),
      };
      localStorage.setItem("chatbot_user", JSON.stringify(user));
      navigate("/");
    } catch (err: unknown) {
      setError(err instanceof Error ? err.message : "Login failed");
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <div className="relative min-h-screen flex items-center justify-center overflow-hidden bg-[#fcfdfd]">

      {/* ── Dot grid texture ── */}
      <div
        className="absolute inset-0 z-0 opacity-20"
        style={{
          backgroundImage: "radial-gradient(circle, #334155 1px, transparent 1px)",
          backgroundSize: "28px 28px",
        }}
      />

      {/* ── Gradient orbs — glowing on dark bg ── */}
      {/* Cyan — top left */}
      <div
        className="absolute -top-32 -left-32 w-[520px] h-[520px] rounded-full blur-3xl opacity-[0.18] animate-pulse"
        style={{ background: "#0ea5e9", animationDuration: "5s" }}
      />
      {/* Orange — bottom right */}
      <div
        className="absolute -bottom-40 -right-40 w-[560px] h-[560px] rounded-full blur-3xl opacity-[0.16] animate-pulse"
        style={{ background: "#f97316", animationDuration: "7s" }}
      />
      {/* Green — top right */}
      <div
        className="absolute -top-24 right-1/4 w-[320px] h-[320px] rounded-full blur-3xl opacity-[0.12] animate-pulse"
        style={{ background: "#10b981", animationDuration: "9s" }}
      />
      {/* Purple accent — center left */}
      <div
        className="absolute top-1/2 -left-40 w-[360px] h-[360px] rounded-full blur-3xl opacity-[0.10] animate-pulse"
        style={{ background: "#8b5cf6", animationDuration: "11s" }}
      />

      {/* ── Neural network — bottom left ── */}
      <svg
        className="absolute bottom-8 left-8 opacity-[0.35] animate-float-slow"
        width="200" height="200" viewBox="0 0 200 200"
        fill="none" xmlns="http://www.w3.org/2000/svg"
      >
        <line x1="20" y1="150" x2="65" y2="90" stroke="#0ea5e9" strokeWidth="1"/>
        <line x1="20" y1="150" x2="55" y2="170" stroke="#0ea5e9" strokeWidth="1"/>
        <line x1="65" y1="90" x2="110" y2="125" stroke="#0ea5e9" strokeWidth="1"/>
        <line x1="65" y1="90" x2="130" y2="55" stroke="#10b981" strokeWidth="1"/>
        <line x1="110" y1="125" x2="170" y2="105" stroke="#0ea5e9" strokeWidth="1"/>
        <line x1="130" y1="55" x2="170" y2="105" stroke="#10b981" strokeWidth="1"/>
        <line x1="130" y1="55" x2="175" y2="30" stroke="#10b981" strokeWidth="1"/>
        <line x1="55" y1="170" x2="110" y2="125" stroke="#0ea5e9" strokeWidth="1"/>
        <line x1="175" y1="30" x2="170" y2="105" stroke="#10b981" strokeWidth="1"/>
        <circle cx="20" cy="150" r="3.5" fill="#0ea5e9" fillOpacity="0.7"/>
        <circle cx="55" cy="170" r="2.5" fill="#0ea5e9" fillOpacity="0.5"/>
        <circle cx="65" cy="90" r="5" fill="#0ea5e9" fillOpacity="0.9"/>
        <circle cx="110" cy="125" r="3.5" fill="#0ea5e9" fillOpacity="0.7"/>
        <circle cx="130" cy="55" r="5" fill="#10b981" fillOpacity="0.9"/>
        <circle cx="170" cy="105" r="3.5" fill="#10b981" fillOpacity="0.7"/>
        <circle cx="175" cy="30" r="2.5" fill="#10b981" fillOpacity="0.5"/>
      </svg>

      {/* ── Neural network — top right ── */}
      <svg
        className="absolute top-8 right-8 opacity-[0.30] animate-float"
        style={{ "--float-rotate": "0deg" } as React.CSSProperties}
        width="180" height="180" viewBox="0 0 180 180"
        fill="none" xmlns="http://www.w3.org/2000/svg"
      >
        <line x1="160" y1="30" x2="115" y2="80" stroke="#f97316" strokeWidth="1"/>
        <line x1="115" y1="80" x2="70" y2="55" stroke="#f97316" strokeWidth="1"/>
        <line x1="115" y1="80" x2="130" y2="135" stroke="#0ea5e9" strokeWidth="1"/>
        <line x1="70" y1="55" x2="25" y2="90" stroke="#f97316" strokeWidth="1"/>
        <line x1="25" y1="90" x2="80" y2="155" stroke="#0ea5e9" strokeWidth="1"/>
        <line x1="80" y1="155" x2="130" y2="135" stroke="#0ea5e9" strokeWidth="1"/>
        <line x1="70" y1="55" x2="30" y2="25" stroke="#f97316" strokeWidth="1"/>
        <circle cx="160" cy="30" r="3.5" fill="#f97316" fillOpacity="0.7"/>
        <circle cx="115" cy="80" r="5" fill="#f97316" fillOpacity="0.9"/>
        <circle cx="70" cy="55" r="3.5" fill="#f97316" fillOpacity="0.7"/>
        <circle cx="130" cy="135" r="2.5" fill="#0ea5e9" fillOpacity="0.5"/>
        <circle cx="25" cy="90" r="3.5" fill="#0ea5e9" fillOpacity="0.7"/>
        <circle cx="80" cy="155" r="2.5" fill="#0ea5e9" fillOpacity="0.5"/>
        <circle cx="30" cy="25" r="2.5" fill="#f97316" fillOpacity="0.5"/>
      </svg>


      {/* ── Rotated square — bottom left ── */}
      <div
        className="absolute bottom-16 left-16 w-24 h-24 border border-slate-200 opacity-50 animate-float"
        style={{ transform: "rotate(20deg)", "--float-rotate": "20deg" } as React.CSSProperties}
      />
      <div
        className="absolute bottom-28 left-28 w-12 h-12 border border-slate-200 opacity-40"
        style={{ transform: "rotate(45deg)" }}
      />

      {/* ── Hexagon — bottom right ── */}
      <svg
        className="absolute bottom-12 right-16 opacity-[0.35] animate-float"
        style={{ "--float-rotate": "0deg", animationDelay: "2s" } as React.CSSProperties}
        width="80" height="80" viewBox="0 0 160 160"
        fill="none" xmlns="http://www.w3.org/2000/svg"
      >
        <path
          d="M 140 80 L 110 132 L 50 132 L 20 80 L 50 28 L 110 28 Z"
          stroke="#f97316" strokeWidth="1.5" fill="none"
        />
        <path
          d="M 122 80 L 101 114 L 59 114 L 38 80 L 59 46 L 101 46 Z"
          stroke="#f97316" strokeWidth="1" strokeOpacity="0.5" fill="none"
        />
      </svg>


      {/* ── Small floating dots ── */}
      <div className="absolute top-1/4 left-12 w-2 h-2 rounded-full bg-sky-400 opacity-60 animate-float" style={{ animationDelay: "1s" }} />
      <div className="absolute top-1/3 right-20 w-1.5 h-1.5 rounded-full bg-orange-400 opacity-60 animate-float-slow" style={{ animationDelay: "3s" }} />
      <div className="absolute bottom-1/3 left-1/4 w-1.5 h-1.5 rounded-full bg-emerald-400 opacity-60 animate-float" style={{ animationDelay: "0.5s" }} />
      <div className="absolute top-2/3 right-1/3 w-1 h-1 rounded-full bg-violet-400 opacity-50 animate-float-slow" style={{ animationDelay: "4s" }} />

      {/* ── Login card ── */}
      <div className="relative z-10 w-full max-w-md xl:max-w-lg 2xl:max-w-xl px-4">
        <div className="bg-white rounded-2xl shadow-2xl shadow-black/40 p-7 xl:p-10 border border-white/10">

          {/* Brand logo */}
          <div className="flex flex-col items-center gap-2 xl:gap-4 mb-6 xl:mb-8">
            <img src={logoUrl} alt="Maintiva" className="w-12 h-12 xl:w-20 xl:h-20 object-contain" />
            <div className="text-center">
              <h1 className="text-xl xl:text-3xl font-semibold text-slate-900">Maintiva Agent</h1>
              <p className="text-slate-400 text-sm xl:text-base mt-0.5 xl:mt-1">Welcome back to your AI Based Virtual Agent for Analysis, Learning & RCA</p>
            </div>
          </div>

          <form onSubmit={handleLogin} className="space-y-4 xl:space-y-5">
            <div>
              <label htmlFor="email" className="block text-xs xl:text-sm font-medium mb-1.5 text-slate-600">
                Email Address
              </label>
              <input
                id="email"
                type="email"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                className="w-full px-3 xl:px-4 py-2 xl:py-3 text-sm xl:text-base rounded-xl border border-slate-200 bg-slate-50 focus:outline-none focus:ring-2 focus:ring-sky-400/30 focus:border-sky-400 placeholder:text-slate-300 transition"
                placeholder="you@example.com"
                disabled={isLoading}
              />
            </div>

            <div>
              <label htmlFor="password" className="block text-xs xl:text-sm font-medium mb-1.5 text-slate-600">
                Password
              </label>
              <div className="relative">
                <input
                  id="password"
                  type={showPassword ? "text" : "password"}
                  value={password}
                  onChange={(e) => setPassword(e.target.value)}
                  className="w-full px-3 xl:px-4 py-2 xl:py-3 pr-10 xl:pr-12 text-sm xl:text-base rounded-xl border border-slate-200 bg-slate-50 focus:outline-none focus:ring-2 focus:ring-sky-400/30 focus:border-sky-400 placeholder:text-slate-300 transition"
                  placeholder="Enter your password"
                  disabled={isLoading}
                />
                <button
                  type="button"
                  onClick={() => setShowPassword((v) => !v)}
                  className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 transition"
                  tabIndex={-1}
                  aria-label={showPassword ? "Hide password" : "Show password"}
                >
                  {showPassword ? <EyeOff className="w-4 h-4 xl:w-5 xl:h-5" /> : <Eye className="w-4 h-4 xl:w-5 xl:h-5" />}
                </button>
              </div>
            </div>

            {error && (
              <div className="bg-red-50 border border-red-100 text-red-600 px-3 py-2 rounded-xl text-xs xl:text-sm">
                {error}
              </div>
            )}

            <button
              type="submit"
              disabled={isLoading}
              className="w-full flex items-center justify-center gap-2 bg-[#059669] hover:bg-[#047857] active:bg-[#065F46] text-white py-2.5 xl:py-3.5 text-sm xl:text-base rounded-xl transition font-medium disabled:opacity-50 disabled:cursor-not-allowed mt-1"
            >
              {isLoading ? (
                <Loader2 className="w-4 h-4 xl:w-5 xl:h-5 animate-spin" />
              ) : (
                <LogIn className="w-4 h-4 xl:w-5 xl:h-5" />
              )}
              {isLoading ? "Signing in…" : "Sign In"}
            </button>
          </form>
        </div>
      </div>
    </div>
  );
}
