import React, { useState } from 'react';
import { AuthProvider, useAuth } from './AdminAuth';

function LoginFormInner() {
  const { login } = useAuth();
  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();
    setError('');
    setLoading(true);
    const ok = await login(email, password);
    setLoading(false);
    if (ok) window.location.href = '/admin';
    else setError('Ongeldige inloggegevens');
  };

  return (
    <div className="min-h-screen bg-[#0A0A0A] flex items-center justify-center">
      <div className="w-full max-w-sm">
        <div className="text-center mb-8">
          <h1 className="text-2xl font-bold text-[#F5F5F5] mb-1">🔒 SneakerPicks</h1>
          <p className="text-sm text-[#999]">Admin Panel</p>
        </div>
        <form onSubmit={handleSubmit} className="bg-[#141414] border border-[#333] rounded-xl p-6 space-y-4">
          {error && <div className="bg-[#FF3B3B]/10 text-[#FF3B3B] text-sm px-4 py-2 rounded-lg">{error}</div>}
          <div>
            <label className="block text-sm text-[#999] mb-1.5">Email / Username</label>
            <input type="text" value={email} onChange={e => setEmail(e.target.value)}
              className="w-full bg-[#0A0A0A] border border-[#333] rounded-lg px-4 py-2.5 text-[#F5F5F5] focus:outline-none focus:border-[#00FF6A] transition-colors"
              placeholder="admin" autoFocus />
          </div>
          <div>
            <label className="block text-sm text-[#999] mb-1.5">Password</label>
            <input type="password" value={password} onChange={e => setPassword(e.target.value)}
              className="w-full bg-[#0A0A0A] border border-[#333] rounded-lg px-4 py-2.5 text-[#F5F5F5] focus:outline-none focus:border-[#00FF6A] transition-colors"
              placeholder="••••••••" />
          </div>
          <button type="submit" disabled={loading}
            className="w-full bg-[#00FF6A] text-[#0A0A0A] font-semibold py-2.5 rounded-lg hover:bg-[#00CC55] transition-colors disabled:opacity-50">
            {loading ? 'Bezig...' : 'Inloggen'}
          </button>
        </form>
      </div>
    </div>
  );
}

export default function LoginForm() {
  return <AuthProvider><LoginFormInner /></AuthProvider>;
}
