"use client";

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

export default function RegisterPage() {
  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/register", {
      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 ?? "Registration 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-circle top-20 right-20 opacity-50 animate-float" />
      <div className="geo-accent geo-square bottom-32 left-16 opacity-30" style={{ animationDelay: "3s" }} />
      <div 
        className="geo-accent geo-line bottom-1/4 -left-10 opacity-40 hidden lg:block" 
        style={{ transform: "rotate(45deg)" }} 
      />
      <div 
        className="geo-accent top-1/4 right-1/4 opacity-20 hidden md:block"
        style={{ width: "60px", height: "60px", border: "4px solid var(--accent-cyan)" }}
      />
      
      {/* Accent Glow - Magenta variant */}
      <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-magenta) 0%, transparent 70%)" }}
      />

      {/* Register Card */}
      <div className="w-full max-w-md brutal-card-magenta p-8 md:p-10 relative z-10 animate-slide-up" style={{ boxShadow: "8px 8px 0 var(--accent-magenta)" }}>
        {/* Header */}
        <div className="mb-8">
          <div className="flex items-center gap-3 mb-4">
            <div className="w-3 h-3 bg-accent-magenta" />
            <div className="w-3 h-3 bg-accent-cyan" />
            <div className="w-3 h-3 bg-accent-lime" />
          </div>
          <h1 className="text-3xl md:text-4xl font-black text-text-primary tracking-tight">
            Create account
          </h1>
          <p className="mt-2 text-text-secondary text-sm">
            Register to start uploading your 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"
              style={{ borderBottomColor: "var(--accent-magenta)" }}
            />
          </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="Min. 6 characters"
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              required
              minLength={6}
              autoComplete="new-password"
              style={{ borderBottomColor: "var(--accent-magenta)" }}
            />
            <p className="mt-2 text-xs text-text-muted">
              Use at least 6 characters for security
            </p>
          </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"
              style={{ 
                background: "var(--accent-magenta)", 
                boxShadow: "4px 4px 0 var(--bg-dark)" 
              }}
              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>
                  Creating account...
                </>
              ) : (
                <>
                  Create Account
                  <svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M18 9v3m0 0v3m0-3h3m-3 0h-3m-2-5a4 4 0 11-8 0 4 4 0 018 0zM3 20a6 6 0 0112 0v1H3v-1z" />
                  </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">
            Already have an account?{" "}
            <Link 
              className="brutal-link" 
              href="/login"
              style={{ color: "var(--accent-magenta)" }}
            >
              Log in
            </Link>
          </p>
        </div>
      </div>
    </div>
  );
}
