import React, { useState } from 'react';
import axios from 'axios';

function Login({ onLoginSuccess }) {
  const [loginForm, setLoginForm] = useState({ username: '', password: '' });
  const [error, setError] = useState('');
  const [loading, setLoading] = useState(false);

  const handleLogin = async (e) => {
    e.preventDefault();
    setError('');
    setLoading(true);
    try {
      // Points exactly to your modular auth backend router
      const res = await axios.post('http://localhost:5000/api/auth/login', loginForm);
      onLoginSuccess(res.data.username);
    } catch (err) {
      setError(err.response?.data?.error || 'Authentication failed. Please verify credentials.');
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="min-h-screen bg-slate-100 flex items-center justify-center p-4">
      <div className="max-w-md w-full bg-white rounded-xl shadow-lg overflow-hidden transition-all duration-300 hover:shadow-2xl border border-gray-100">
        <div className="bg-indigo-600 p-6 text-center">
          <h2 className="text-2xl font-black text-white tracking-wide uppercase">DAB Enterprise Ltd</h2>
          <p className="text-indigo-200 text-xs mt-1">Business Sales Management Solution (BWS)</p>
        </div>
        
        <form onSubmit={handleLogin} className="p-8 space-y-5">
          {error && (
            <div className="p-3 bg-rose-50 text-rose-600 text-sm font-semibold rounded-lg border border-rose-200">
              {error}
            </div>
          )}

          <div>
            <label className="block text-xs font-bold text-gray-600 uppercase tracking-wider mb-1">
              Username
            </label>
            <input 
              type="text" 
              placeholder="Enter username" 
              className="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none transition-all text-gray-700 bg-slate-50" 
              onChange={e => setLoginForm({...loginForm, username: e.target.value})} 
              required 
            />
          </div>

          <div>
            <label className="block text-xs font-bold text-gray-600 uppercase tracking-wider mb-1">
              Password
            </label>
            <input 
              type="password" 
              placeholder="••••••••" 
              className="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none transition-all text-gray-700 bg-slate-50" 
              onChange={e => setLoginForm({...loginForm, password: e.target.value})} 
              required 
            />
          </div>

          <button 
            type="submit" 
            disabled={loading}
            className="w-full bg-indigo-600 text-white font-bold py-3 rounded-lg hover:bg-indigo-700 active:scale-95 transition-all focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 disabled:bg-indigo-400 disabled:scale-100 shadow-md"
          >
            {loading ? 'Authorizing Access...' : 'Login to System'}
          </button>
        </form>
      </div>
    </div>
  );
}

export default Login;