"use client";

import Link from "next/link";
import { useRouter } from "next/navigation";
import { FormEvent, useState } from "react";

export default function LoginPage() {
  const router = useRouter();
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState<string | null>(null);
  const [loading, setLoading] = useState(false);

  const onSubmit = async (e: FormEvent) => {
    e.preventDefault();
    setError(null);
    setLoading(true);
    const res = await fetch("/api/auth/login", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ email, password }),
    });
    const data = await res.json();
    setLoading(false);
    if (!res.ok) {
      setError(data?.error ?? "Login failed");
      return;
    }
    router.push("/dashboard");
  };

  return (
    <div className="min-h-screen flex items-center justify-center px-4 py-8 relative overflow-hidden">
      {/* Geometric Decorations */}
      <div className="geo-accent geo-square top-20 left-10 opacity-50 animate-float" />
      <div className="geo-accent geo-circle bottom-20 right-10 opacity-30" style={{ animationDelay: "2s" }} />
      <div className="geo-accent geo-square bottom-40 left-1/4 opacity-20 hidden md:block" style={{ width: "40px", height: "40px" }} />
      <div 
        className="geo-accent geo-line top-1/3 -right-10 opacity-40 hidden lg:block" 
        style={{ transform: "rotate(-45deg)" }} 
      />
      
      {/* Accent Glow */}
      <div 
        className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[600px] h-[600px] rounded-full opacity-10 pointer-events-none"
        style={{ background: "radial-gradient(circle, var(--accent-lime) 0%, transparent 70%)" }}
      />

      {/* Login Card */}
      <div className="w-full max-w-md brutal-card p-8 md:p-10 relative z-10 animate-slide-up">
        {/* Header */}
        <div className="mb-8">
          <div className="flex items-center gap-3 mb-4">
            <div className="w-3 h-3 bg-accent-lime" />
            <div className="w-3 h-3 bg-accent-magenta" />
            <div className="w-3 h-3 bg-accent-cyan" />
          </div>
          <h1 className="text-3xl md:text-4xl font-black text-text-primary tracking-tight">
            Welcome back
          </h1>
          <p className="mt-2 text-text-secondary text-sm">
            Log in to upload and download files.
          </p>
        </div>

        {/* Form */}
        <form className="space-y-6" onSubmit={onSubmit}>
          <div className="animate-slide-up delay-100" style={{ opacity: 0, animationFillMode: "forwards" }}>
            <label className="brutal-label">Email Address</label>
            <input
              type="email"
              className="brutal-input"
              placeholder="you@example.com"
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              required
              autoComplete="email"
            />
          </div>
          
          <div className="animate-slide-up delay-200" style={{ opacity: 0, animationFillMode: "forwards" }}>
            <label className="brutal-label">Password</label>
            <input
              type="password"
              className="brutal-input"
              placeholder="Enter your password"
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              required
              autoComplete="current-password"
            />
          </div>

          {error && (
            <div className="flex items-center gap-2 p-3 bg-danger-soft border-l-4 border-danger">
              <svg className="w-5 h-5 text-danger flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
              </svg>
              <p className="text-sm text-danger font-medium">{error}</p>
            </div>
          )}

          <div className="animate-slide-up delay-300 pt-2" style={{ opacity: 0, animationFillMode: "forwards" }}>
            <button
              type="submit"
              className="brutal-btn w-full"
              disabled={loading}
            >
              {loading ? (
                <>
                  <svg className="animate-spin h-5 w-5" viewBox="0 0 24 24">
                    <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" fill="none" />
                    <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" />
                  </svg>
                  Signing in...
                </>
              ) : (
                <>
                  Sign In
                  <svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 8l4 4m0 0l-4 4m4-4H3" />
                  </svg>
                </>
              )}
            </button>
          </div>
        </form>

        {/* Footer */}
        <div className="mt-8 pt-6 border-t-2 border-bg-elevated">
          <p className="text-sm text-text-secondary text-center">
            No account yet?{" "}
            <Link className="brutal-link" href="/register">
              Create one
            </Link>
          </p>
        </div>
      </div>
    </div>
  );
}
