"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { ShieldCheck } from "lucide-react";

export default function RegisterPage() {
  const router = useRouter();
  const [name, setName] = useState("");
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);

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

    try {
      const res = await fetch("/api/auth/register", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ name, email, password }),
      });

      if (!res.ok) {
        const data = await res.json();
        setError(data.message || "Registration failed");
        setLoading(false);
        return;
      }

      router.push("/login");
    } catch (err) {
      setError("An error occurred. Please try again.");
      setLoading(false);
    }
  };

  return (
    <div className="min-h-screen bg-muted/30 pt-32 pb-24 flex flex-col items-center justify-center">
      
      <div className="mb-8 flex items-center justify-center">
        <div className="font-black text-4xl tracking-tighter flex items-center text-foreground">
          <span>K </span>
          <span className="mx-2 text-brand-accent">CREATION</span>
        </div>
      </div>

      <div className="w-full max-w-md bg-card rounded-xl border border-border/50 p-8 shadow-sm">
        <h1 className="text-3xl font-black text-foreground mb-6">Créer un compte</h1>
        
        {error && (
          <div className="bg-red-50 dark:bg-red-900/20 text-red-600 border border-red-200 dark:border-red-800 p-4 rounded-md mb-6 text-sm flex items-center gap-2">
            <ShieldCheck className="w-5 h-5 flex-shrink-0" />
            {error}
          </div>
        )}

        <form onSubmit={handleSubmit} className="space-y-4">
          <div>
            <label className="block text-sm font-bold text-gray-700 dark:text-gray-300 mb-1">Votre nom</label>
            <input 
              type="text" 
              required
              value={name}
              onChange={(e) => setName(e.target.value)}
              placeholder="Prénom et nom"
              className="w-full bg-white dark:bg-black/40 border border-gray-300 dark:border-white/10 rounded-md px-4 py-2.5 focus:outline-none focus:ring-2 focus:ring-brand-accent focus:border-transparent text-foreground" 
            />
          </div>
          <div>
            <label className="block text-sm font-bold text-gray-700 dark:text-gray-300 mb-1">E-mail</label>
            <input 
              type="email" 
              required
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              className="w-full bg-white dark:bg-black/40 border border-gray-300 dark:border-white/10 rounded-md px-4 py-2.5 focus:outline-none focus:ring-2 focus:ring-brand-accent focus:border-transparent text-foreground" 
            />
          </div>
          <div>
            <label className="block text-sm font-bold text-gray-700 dark:text-gray-300 mb-1">Mot de passe</label>
            <input 
              type="password" 
              required
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              placeholder="Au moins 6 caractères"
              className="w-full bg-white dark:bg-black/40 border border-gray-300 dark:border-white/10 rounded-md px-4 py-2.5 focus:outline-none focus:ring-2 focus:ring-brand-accent focus:border-transparent text-foreground" 
            />
            <div className="text-xs text-gray-500 mt-1 flex items-center gap-1">
              <i>i</i> Le mot de passe doit comporter au moins 6 caractères.
            </div>
          </div>

          <button 
            type="submit" 
            disabled={loading}
            className="w-full bg-brand-accent hover:bg-red-700 text-white py-2.5 rounded-md font-bold transition-colors shadow-sm disabled:opacity-50 mt-4"
          >
            {loading ? "Création du compte..." : "Continuer"}
          </button>
        </form>

        <div className="mt-6 pt-6 border-t border-border/50 text-sm">
          Vous avez déjà un compte ? <Link href="/login" className="text-brand-accent hover:underline font-semibold">Connectez-vous</Link>
        </div>
      </div>
    </div>
  );
}
