import { Button } from "@/components/ui/button";
import { NavigationMenu, NavigationMenuContent, NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuTrigger } from "@/components/ui/navigation-menu";
import { Menu, X } from "lucide-react";
import { useState, useEffect } from "react";
import { useNavigate, useLocation } from "react-router-dom";

const Navigation = () => {
  const [isOpen, setIsOpen] = useState(false);
  const [isScrolled, setIsScrolled] = useState(false);
  const navigate = useNavigate();
  const location = useLocation();
  const isFoundersPage = location.pathname === '/founders';

  useEffect(() => {
    const handleScroll = () => {
      const scrollPosition = window.scrollY;
      setIsScrolled(scrollPosition > 50);
    };

    window.addEventListener('scroll', handleScroll);
    return () => window.removeEventListener('scroll', handleScroll);
  }, []);

  const scrollToSection = (sectionId: string) => {
    const element = document.getElementById(sectionId);
    if (element) {
      element.scrollIntoView({ behavior: 'smooth' });
    }
    setIsOpen(false); // Close mobile menu after navigation
  };

  const getStartedPath = isFoundersPage ? '/founders-start' : '/start';

  return (
    <nav className={`fixed top-0 z-50 w-full transition-all duration-500 ease-out ${isScrolled ? 'py-2 px-4' : 'py-0 px-0'}`}>
      <div className={`mx-auto max-w-7xl transition-all duration-500 ease-out ${isScrolled ? 'px-6 rounded-2xl border border-white/20 shadow-2xl shadow-black/10 backdrop-blur-xl bg-white/80 floating-glass' : 'px-6 lg:px-8 bg-transparent backdrop-blur supports-[backdrop-filter]:bg-transparent border-transparent'}`}>
        <div className="flex h-16 items-center justify-between">
          {/* Logo */}
          <div className="flex items-center">
            <a href="/" className="flex items-center gap-0.25">
              <img src="/lovable-uploads/6db08549-a612-419f-b065-cf8f6eed00d4.png" alt="Stacked Logo" className="h-8 w-8" />
              <span className="font-bold text-xl">
                <span className={`transition-colors duration-300 ${isScrolled ? 'text-blue-950' : 'text-white'}`}>Stackd</span>
              </span>
            </a>
          </div>

          {/* Desktop Navigation */}
          <div className="hidden lg:block">
            <NavigationMenu>
              <NavigationMenuList>
                <NavigationMenuItem>
                  <NavigationMenuTrigger className={`bg-transparent transition-colors duration-300 ${isScrolled ? 'hover:bg-accent/50 text-gray-900' : 'hover:bg-white/10 text-white'}`}>
                    Find Mentors
                  </NavigationMenuTrigger>
                  <NavigationMenuContent>
                    <div className="grid gap-3 p-6 w-[400px]">
                      <NavigationMenuLink 
                        className="block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground cursor-pointer"
                        onClick={() => window.location.href = 'https://app.stackd.org'}
                      >
                        <div className="text-sm font-medium leading-none">Customer Success</div>
                        <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
                          Master customer retention and growth strategies
                        </p>
                      </NavigationMenuLink>
                      <NavigationMenuLink 
                        className="block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground cursor-pointer"
                        onClick={() => window.location.href = 'https://app.stackd.org'}
                      >
                        <div className="text-sm font-medium leading-none">Sales</div>
                        <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
                          Connect with VP Sales and Sales Directors
                        </p>
                      </NavigationMenuLink>
                      <NavigationMenuLink 
                        className="block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground cursor-pointer"
                        onClick={() => window.location.href = 'https://app.stackd.org'}
                      >
                        <div className="text-sm font-medium leading-none">Marketing</div>
                        <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
                          Learn from CMOs and Growth Marketing professionals
                        </p>
                      </NavigationMenuLink>
                      <NavigationMenuLink 
                        className="block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground cursor-pointer"
                        onClick={() => window.location.href = 'https://app.stackd.org'}
                      >
                        <div className="text-sm font-medium leading-none">Growth</div>
                        <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
                          Get insights from Growth experts and Growth Marketers
                        </p>
                      </NavigationMenuLink>
                      <NavigationMenuLink 
                        className="block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground cursor-pointer"
                        onClick={() => window.location.href = 'https://app.stackd.org'}
                      >
                        <div className="text-sm font-medium leading-none">RevOps</div>
                        <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
                          Get guidance on revenue operations and strategy
                        </p>
                      </NavigationMenuLink>
                      <NavigationMenuLink 
                        className="block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground cursor-pointer"
                        onClick={() => window.location.href = 'https://app.stackd.org'}
                      >
                        <div className="text-sm font-medium leading-none">Product Marketing</div>
                        <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
                          Learn positioning, messaging, and go-to-market strategy
                        </p>
                      </NavigationMenuLink>
                    </div>
                  </NavigationMenuContent>
                </NavigationMenuItem>
                
                 <NavigationMenuItem>
                  <NavigationMenuLink 
                    asChild
                    className={`group inline-flex h-10 w-max items-center justify-center rounded-md bg-transparent px-4 py-2 text-sm font-medium transition-colors focus:outline-none disabled:pointer-events-none disabled:opacity-50 cursor-pointer ${isScrolled ? 'hover:bg-accent/50 hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground text-gray-900' : 'hover:bg-white/10 hover:text-white focus:bg-white/10 focus:text-white text-white'}`}
                  >
                    <a href="https://app.stackd.org">Become a mentor</a>
                  </NavigationMenuLink>
                </NavigationMenuItem>
                
                <NavigationMenuItem>
                  <NavigationMenuLink 
                    asChild
                    className={`group inline-flex h-10 w-max items-center justify-center rounded-md bg-transparent px-4 py-2 text-sm font-medium transition-colors focus:outline-none disabled:pointer-events-none disabled:opacity-50 cursor-pointer ${isScrolled ? 'hover:bg-accent/50 hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground text-gray-900' : 'hover:bg-white/10 hover:text-white focus:bg-white/10 focus:text-white text-white'}`}
                  >
                    <a href="/podcasts">Podcasts</a>
                  </NavigationMenuLink>
                </NavigationMenuItem>
                
                <NavigationMenuItem>
                  <NavigationMenuLink 
                    asChild
                    className={`group inline-flex h-10 w-max items-center justify-center rounded-md bg-transparent px-4 py-2 text-sm font-medium transition-colors focus:outline-none disabled:pointer-events-none disabled:opacity-50 cursor-pointer ${isScrolled ? 'hover:bg-accent/50 hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground text-gray-900' : 'hover:bg-white/10 hover:text-white focus:bg-white/10 focus:text-white text-white'}`}
                  >
                    <a href="/blog">Blog</a>
                  </NavigationMenuLink>
                </NavigationMenuItem>
                
                <NavigationMenuItem>
                  <NavigationMenuLink 
                    asChild
                    className={`group inline-flex h-10 w-max items-center justify-center rounded-md bg-transparent px-4 py-2 text-sm font-medium transition-colors focus:outline-none disabled:pointer-events-none disabled:opacity-50 cursor-pointer ${isScrolled ? 'hover:bg-accent/50 hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground text-gray-900' : 'hover:bg-white/10 hover:text-white focus:bg-white/10 focus:text-white text-white'}`}
                  >
                    <a href="/#pricing" onClick={(e) => {
                      if (location.pathname === '/') {
                        e.preventDefault(); 
                        scrollToSection('pricing');
                      }
                    }}>Pricing</a>
                  </NavigationMenuLink>
                </NavigationMenuItem>
              </NavigationMenuList>
            </NavigationMenu>
          </div>

          {/* Desktop CTA Buttons */}
          <div className="hidden lg:flex items-center gap-4">
            <Button 
              variant="ghost" 
              className={`transition-colors duration-300 ${isScrolled ? 'text-gray-900 hover:bg-accent hover:text-accent-foreground' : 'text-white hover:bg-white/10 hover:text-white'}`}
              onClick={() => window.open('https://app.stackd.org', '_self')}
            >
              Sign in
            </Button>
            <Button 
              className={`transition-all duration-300 ${isScrolled ? 'bg-blue-950 hover:bg-blue-900 text-white' : 'bg-white/20 hover:bg-white/30 text-white border border-white/30'}`}
              onClick={() => window.location.href = 'https://app.stackd.org'}
            >
              Get Started
            </Button>
          </div>

          {/* Mobile menu button */}
          <div className="lg:hidden">
            <Button 
              variant="ghost" 
              size="icon" 
              onClick={() => setIsOpen(!isOpen)} 
              aria-label="Toggle menu"
              className={`transition-colors duration-300 ${isScrolled ? 'text-gray-900 hover:bg-accent' : 'text-white hover:bg-white/10'}`}
            >
              {isOpen ? <X className="h-6 w-6" /> : <Menu className="h-6 w-6" />}
            </Button>
          </div>
        </div>

        {/* Mobile Navigation */}
        {isOpen && (
          <div className="lg:hidden border-t border-white/20 bg-white/95 backdrop-blur-md rounded-b-2xl shadow-lg">
            <div className="px-4 pt-4 pb-6 space-y-2">
              <a href="/#mentors" className="block px-4 py-3 text-base font-medium rounded-lg text-gray-700 hover:text-gray-900 hover:bg-gray-100 transition-colors">
                Find Mentors
              </a>
              <a 
                href="https://app.stackd.org"
                className="block px-4 py-3 text-base font-medium rounded-lg text-gray-700 hover:text-gray-900 hover:bg-gray-100 transition-colors"
              >
                Become a mentor
              </a>
              <a 
                href="/podcasts" 
                className="block px-4 py-3 text-base font-medium rounded-lg text-gray-700 hover:text-gray-900 hover:bg-gray-100 transition-colors"
              >
                Podcasts
              </a>
              <a 
                href="/blog" 
                className="block px-4 py-3 text-base font-medium rounded-lg text-gray-700 hover:text-gray-900 hover:bg-gray-100 transition-colors"
              >
                Blog
              </a>
              <a 
                href="/#pricing"
                onClick={(e) => {
                  if (location.pathname === '/') {
                    e.preventDefault(); 
                    scrollToSection('pricing');
                  }
                }}
                className="block px-4 py-3 text-base font-medium rounded-lg text-gray-700 hover:text-gray-900 hover:bg-gray-100 transition-colors"
              >
                Pricing
              </a>
              <div className="pt-4 border-t border-gray-200 mt-4">
                <div className="flex flex-col gap-3">
                  <Button 
                    variant="ghost" 
                    className="justify-start text-gray-900 hover:bg-gray-100"
                    onClick={() => window.open('https://app.stackd.org', '_self')}
                  >
                    Sign In
                  </Button>
                  <Button 
                    className="justify-start bg-blue-950 hover:bg-blue-900 text-white"
                    onClick={() => window.location.href = 'https://app.stackd.org'}
                  >
                    Get Started
                  </Button>
                </div>
              </div>
            </div>
          </div>
        )}
      </div>

      {/* ... keep existing code (floating-glass styles) */}
      <style>{`
        .floating-glass {
          background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.7) 100%) !important;
          border: 1px solid rgba(255, 255, 255, 0.3) !important;
          box-shadow: 
            0 8px 32px rgba(0, 0, 0, 0.1),
            0 4px 16px rgba(0, 0, 0, 0.05),
            inset 0 1px 0 rgba(255, 255, 255, 0.4) !important;
          backdrop-filter: blur(20px) !important;
          -webkit-backdrop-filter: blur(20px) !important;
        }
        
        .floating-glass::before {
          content: '';
          position: absolute;
          top: -1px;
          left: -1px;
          right: -1px;
          bottom: -1px;
          background: linear-gradient(135deg, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.1));
          border-radius: inherit;
          z-index: -1;
          opacity: 0.7;
        }
      `}</style>
    </nav>
  );
};

export default Navigation;
