import { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux';
import { motion, AnimatePresence } from 'framer-motion';
import { RootState, AppDispatch } from '../store';
import { logout } from '../store/authSlice';

export default function Navbar() {
    const { user, token } = useSelector((state: RootState) => state.auth);
    const dispatch = useDispatch<AppDispatch>();
    const navigate = useNavigate();
    const [menuOpen, setMenuOpen] = useState(false);

    const handleLogout = () => {
        dispatch(logout());
        navigate('/');
    };

    return (
        <nav className="fixed top-0 left-0 right-0 z-50 bg-dark-900/80 backdrop-blur-xl border-b border-dark-400/20">
            <div className="max-w-7xl mx-auto px-6 h-16 flex items-center justify-between">
                {/* Logo */}
                <Link to="/" className="flex items-center gap-2 group">
                    <div className="w-8 h-8 bg-gold-gradient rounded-lg flex items-center justify-center
                          group-hover:shadow-gold transition-shadow duration-300">
                        <span className="text-dark-900 font-display font-bold text-sm">D</span>
                    </div>
                    <span className="font-display text-xl font-bold text-light-100">
                        Director<span className="text-gold-500">.AI</span>
                    </span>
                </Link>

                {/* Desktop Navigation */}
                <div className="hidden md:flex items-center gap-1">
                    {token ? (
                        <>
                            <Link to="/dashboard" className="btn-ghost text-sm">Dashboard</Link>
                            <div className="w-px h-6 bg-dark-400/30 mx-2" />
                            <span className="text-sm text-light-500 mr-3">{user?.email}</span>
                            <button onClick={handleLogout} className="btn-ghost text-sm text-light-500 hover:text-red-400">
                                Sign Out
                            </button>
                        </>
                    ) : (
                        <>
                            <a href="#features" className="btn-ghost text-sm">Features</a>
                            <a href="#pricing" className="btn-ghost text-sm">Pricing</a>
                            <a href="#faq" className="btn-ghost text-sm">FAQ</a>
                            <div className="w-px h-6 bg-dark-400/30 mx-2" />
                            <Link to="/login" className="btn-ghost text-sm">Sign In</Link>
                            <Link to="/register" className="btn-primary text-sm ml-2">Get Started</Link>
                        </>
                    )}
                </div>

                {/* Mobile Menu Button */}
                <button
                    onClick={() => setMenuOpen(!menuOpen)}
                    className="md:hidden flex flex-col gap-1.5 p-2"
                    aria-label="Toggle Menu"
                    id="mobile-menu-toggle"
                >
                    <motion.span
                        animate={menuOpen ? { rotate: 45, y: 6 } : { rotate: 0, y: 0 }}
                        className="w-5 h-0.5 bg-light-300 block"
                    />
                    <motion.span
                        animate={menuOpen ? { opacity: 0 } : { opacity: 1 }}
                        className="w-5 h-0.5 bg-light-300 block"
                    />
                    <motion.span
                        animate={menuOpen ? { rotate: -45, y: -6 } : { rotate: 0, y: 0 }}
                        className="w-5 h-0.5 bg-light-300 block"
                    />
                </button>
            </div>

            {/* Mobile Menu */}
            <AnimatePresence>
                {menuOpen && (
                    <motion.div
                        initial={{ height: 0, opacity: 0 }}
                        animate={{ height: 'auto', opacity: 1 }}
                        exit={{ height: 0, opacity: 0 }}
                        transition={{ duration: 0.3 }}
                        className="md:hidden overflow-hidden bg-dark-800/95 backdrop-blur-xl border-b border-dark-400/20"
                    >
                        <div className="px-6 py-4 flex flex-col gap-2">
                            {token ? (
                                <>
                                    <Link to="/dashboard" onClick={() => setMenuOpen(false)} className="btn-ghost text-sm justify-start">Dashboard</Link>
                                    <button onClick={() => { handleLogout(); setMenuOpen(false); }} className="btn-ghost text-sm justify-start text-red-400">Sign Out</button>
                                </>
                            ) : (
                                <>
                                    <a href="#features" onClick={() => setMenuOpen(false)} className="btn-ghost text-sm justify-start">Features</a>
                                    <a href="#pricing" onClick={() => setMenuOpen(false)} className="btn-ghost text-sm justify-start">Pricing</a>
                                    <Link to="/login" onClick={() => setMenuOpen(false)} className="btn-ghost text-sm justify-start">Sign In</Link>
                                    <Link to="/register" onClick={() => setMenuOpen(false)} className="btn-primary text-sm mt-2">Get Started</Link>
                                </>
                            )}
                        </div>
                    </motion.div>
                )}
            </AnimatePresence>
        </nav>
    );
}
