import React, { useState } from 'react';
import { Mail, Lock, User, UtensilsCrossed, ArrowRight } from 'lucide-react';
import { Link, useNavigate } from 'react-router-dom';
import axios from 'axios';

const OwnerLogin = () => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [error, setError] = useState('');
  const navigate = useNavigate();

  const handleLogin = async (e) => {
    e.preventDefault();
    try {
      const { data } = await axios.post('http://localhost:5000/api/auth/login', { 
        email, 
        password,
        role: 'owner' 
      });
      localStorage.setItem('userInfo', JSON.stringify(data));
      navigate('/owner');
    } catch (err) {
      setError(err.response?.data?.message || 'Login failed');
    }
  };

  return (
    <div className="pt-20 min-h-screen bg-[#1a1a1a] flex items-center justify-center p-6">
      <div className="max-w-md w-full bg-white rounded-[40px] shadow-2xl p-12 lg:p-16 border border-gray-100">
        <div className="text-center mb-10">
          <div className="w-20 h-20 bg-primary/10 rounded-3xl flex items-center justify-center mx-auto mb-6 text-primary">
            <UtensilsCrossed size={40} />
          </div>
          <h1 className="text-3xl font-bold font-outfit text-secondary mb-2">Owner Portal</h1>
          <p className="text-gray-500">Manage your restaurant business</p>
        </div>

        {error && <div className="bg-red-50 text-red-500 p-4 rounded-xl mb-6 text-center text-sm font-medium">{error}</div>}

        <form onSubmit={handleLogin} className="space-y-6">
          <div className="space-y-2">
            <label className="text-sm font-semibold text-gray-600 block px-1">Business Email</label>
            <div className="relative">
              <Mail className="absolute left-4 top-3.5 text-gray-400" size={18} />
              <input 
                type="email" 
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                placeholder="owner@restaurant.com"
                className="w-full bg-gray-50 border border-gray-200 rounded-2xl py-3.5 pl-12 pr-4 outline-none focus:border-primary transition-all"
                required
              />
            </div>
          </div>

          <div className="space-y-2">
            <div className="flex justify-between px-1">
              <label className="text-sm font-semibold text-gray-600">Password</label>
              <button type="button" className="text-sm text-primary font-bold">Forgot?</button>
            </div>
            <div className="relative">
              <Lock className="absolute left-4 top-3.5 text-gray-400" size={18} />
              <input 
                type="password" 
                value={password}
                onChange={(e) => setPassword(e.target.value)}
                placeholder="••••••••"
                className="w-full bg-gray-50 border border-gray-200 rounded-2xl py-3.5 pl-12 pr-4 outline-none focus:border-primary transition-all"
                required
              />
            </div>
          </div>

          <button 
            type="submit"
            className="w-full bg-secondary text-white py-4 rounded-2xl font-bold shadow-xl hover:bg-black transition-all flex items-center justify-center gap-2 group"
          >
            Sign In to Dashboard
            <ArrowRight size={18} className="group-hover:translate-x-1 transition-transform" />
          </button>
        </form>

        <div className="mt-10 text-center">
            <p className="text-gray-500 text-sm">
                Need an account for your restaurant? 
                <Link to="/login" className="ml-1 text-primary font-bold hover:underline">Register here</Link>
            </p>
        </div>
      </div>
    </div>
  );
};

export default OwnerLogin;
