// frontend/src/components/Login.jsx
import React, { useState } from 'react';
import axios from 'axios';

export default function Login({ setToken, toggleView }) {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');
  const [error, setError] = useState('');

  const handleSubmit = async (e) => {
    e.preventDefault();
    setError(''); // Clear previous errors
    
    try {
      const res = await axios.post('http://localhost:5000/api/auth/login', { username, password });
      setToken(res.data.token);
      localStorage.setItem('token', res.data.token);
    } catch (err) {
      setError(err.response?.data?.error || 'Invalid system credentials.');
    }
  };

  return (
    <div className="min-h-screen flex items-center justify-center bg-slate-950 px-4">
      <div className="w-full max-w-md bg-white/5 backdrop-blur-lg rounded-2xl border border-white/10 p-8 shadow-2xl">
        <h2 className="text-3xl font-extrabold text-white text-center mb-2">RRBMS Login</h2>
        <p className="text-center text-slate-400 text-sm mb-8">Taste of Rwanda Portal</p>
        
        {error && (
          <div className="mb-4 p-3 bg-rose-500/20 border border-rose-500/30 text-rose-300 rounded-lg text-sm">
            {error}
          </div>
        )}
        
        <form onSubmit={handleSubmit} className="space-y-6">
          <div>
            <label className="block text-xs font-semibold uppercase tracking-wider text-slate-400 mb-2">
              Username
            </label>
            <input 
              type="text" 
              value={username} 
              onChange={e => setUsername(e.target.value)} 
              className="w-full bg-slate-900 border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-amber-500 transition" 
              placeholder="Enter username"
              required 
            />
          </div>
          
          <div>
            <label className="block text-xs font-semibold uppercase tracking-wider text-slate-400 mb-2">
              Password
            </label>
            <input 
              type="password" 
              value={password} 
              onChange={e => setPassword(e.target.value)} 
              className="w-full bg-slate-900 border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-amber-500 transition" 
              placeholder="••••••••"
              required 
            />
          </div>
          
          <button 
            type="submit" 
            className="w-full bg-amber-500 hover:bg-amber-600 text-slate-950 font-bold py-3 px-4 rounded-xl transition shadow-lg shadow-amber-500/20"
          >
            Access Dashboard
          </button>
        </form>

        <div className="mt-6 text-center">
          <button 
            type="button" // CRITICAL: Ensures this acts as a plain link switcher and won't submit the form
            onClick={toggleView} 
            className="text-sm text-amber-400 hover:underline transition bg-transparent border-none cursor-pointer focus:outline-none"
          >
            Don't have an account? Register Staff
          </button>
        </div>
      </div>
    </div>
  );
}