import { Link, NavLink, useNavigate } from "react-router-dom";
import { useAuth } from "../lib/auth";
import { Search, LogOut, Upload, ListVideo } from "lucide-react";
import { useState, useEffect } from "react";

export const Navbar = () => {
  const { user, logout } = useAuth();
  const nav = useNavigate();
  const [scrolled, setScrolled] = useState(false);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 20);
    window.addEventListener("scroll", onScroll);
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const linkClass = ({ isActive }) =>
    `text-sm tracking-wide transition-colors duration-300 ${
      isActive ? "text-white" : "text-[#8A8A8A] hover:text-white"
    }`;

  return (
    <header
      className={`fixed top-0 left-0 right-0 z-50 transition-all duration-300 ${
        scrolled ? "glass border-b" : ""
      }`}
      data-testid="main-navbar"
    >
      <div className="px-6 md:px-12 py-4 flex items-center justify-between">
        <div className="flex items-center gap-10">
          <Link to="/browse" className="flex items-center gap-2" data-testid="nav-logo">
            <span className="font-display text-2xl font-black tracking-tighter text-white">
              Kino
            </span>
            <span className="text-[#D9381E] text-2xl leading-none">.</span>
          </Link>
          {user && (
            <nav className="hidden md:flex items-center gap-7">
              <NavLink to="/browse" className={linkClass} data-testid="nav-browse">Browse</NavLink>
              <NavLink to="/my-list" className={linkClass} data-testid="nav-my-list">My List</NavLink>
              <NavLink to="/requests" className={linkClass} data-testid="nav-requests">Requests</NavLink>
              {user.is_admin && (
                <NavLink to="/admin" className={linkClass} data-testid="nav-admin">Admin</NavLink>
              )}
            </nav>
          )}
        </div>

        {user ? (
          <div className="flex items-center gap-4">
            <button
              onClick={() => nav("/search")}
              className="text-[#8A8A8A] hover:text-white transition-colors duration-300"
              data-testid="nav-search-button"
              aria-label="Search"
            >
              <Search size={18} strokeWidth={1.5} />
            </button>
            {user.is_admin && (
              <button
                onClick={() => nav("/admin/upload")}
                className="hidden sm:flex items-center gap-2 text-xs uppercase tracking-[0.2em] text-[#8A8A8A] hover:text-white transition-colors duration-300"
                data-testid="nav-upload-button"
              >
                <Upload size={14} strokeWidth={1.5} /> Upload
              </button>
            )}
            <div className="flex items-center gap-3 pl-4 border-l border-[#222]">
              <div className="hidden sm:flex flex-col items-end leading-tight">
                <span className="text-xs text-white" data-testid="nav-user-name">{user.name}</span>
                <span className="text-[10px] uppercase tracking-[0.2em] text-[#8A8A8A]">
                  {user.is_admin ? "Admin" : "Member"}
                </span>
              </div>
              <div className="w-8 h-8 bg-[#D9381E] flex items-center justify-center text-white text-xs font-medium">
                {user.name?.[0]?.toUpperCase() || "U"}
              </div>
              <button
                onClick={() => { logout(); nav("/login"); }}
                className="text-[#8A8A8A] hover:text-white transition-colors duration-300"
                data-testid="nav-logout-button"
                aria-label="Logout"
              >
                <LogOut size={16} strokeWidth={1.5} />
              </button>
            </div>
          </div>
        ) : (
          <Link
            to="/login"
            className="text-sm tracking-wide bg-[#D9381E] hover:bg-[#ED4B32] text-white px-5 py-2 transition-colors duration-300"
            data-testid="nav-sign-in"
          >
            Sign in
          </Link>
        )}
      </div>
    </header>
  );
};

export default Navbar;
