"use client";

import Link from "next/link";
import Image from "next/image";
import { usePathname } from "next/navigation";
import { useState, useEffect } from "react";
import { useAuth } from "@/lib/auth-context";
import { useLanguage } from "@/lib/i18n";
import { Button } from "@/components/ui/button";
import { LanguageSwitcher } from "@/components/language-switcher";
import {
  Sheet,
  SheetContent,
  SheetTrigger,
  SheetTitle,
} from "@/components/ui/sheet";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
  type LucideIcon,
  Menu,
  LogOut,
  LayoutDashboard,
  ChevronDown,
  Home,
  Users,
  BookOpenText,
  Info,
  Mail,
} from "lucide-react";
import { cn } from "@/lib/utils";

type NavigationItem = {
  name: string;
  href: string;
  icon: LucideIcon;
  isExternal?: boolean;
};

const navigation: NavigationItem[] = [
  { name: "common.home", href: "/", icon: Home },
  { name: "common.findGuides", href: "/guides", icon: Users },
  { name: "common.blog", href: "/blogs", icon: BookOpenText },
  { name: "common.aboutUs", href: "/#about-us", icon: Info },
  {
    name: "common.contactUs",
    href: "https://uttarakhandtourism.gov.in/page/contact-us",
    icon: Mail,
    isExternal: true,
  },
];

export function Header() {
  const pathname = usePathname();
  const { user, logout, isAuthenticated, isAdmin } = useAuth();
  const { t } = useLanguage();
  const [isOpen, setIsOpen] = useState(false);
  const [scrolled, setScrolled] = useState(false);

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

  const dashboardLink = isAdmin ? "/admin" : "/dashboard";

  return (
    <header
      className={cn(
        "fixed top-0 left-0 right-0 z-50 transition-all duration-500 ease-out",
        scrolled
          ? "bg-background/90 backdrop-blur-2xl border-b border-border/50 shadow-[0_18px_50px_rgba(15,23,42,0.08)]"
          : "bg-transparent",
      )}
    >
      <nav className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
        <div
          className={cn(
            "flex items-center justify-between transition-all duration-500 ease-out",
            scrolled ? "h-16" : "h-20",
          )}
        >
          {/* Logo */}
          <Link href="/" className="block transition-opacity hover:opacity-85">
            <Image
              src="/uttarakhand-logo.png"
              alt="Uttarakhand Tourism Guides"
              width={190}
              height={52}
              className="h-12 w-auto object-contain"
              priority
            />
          </Link>

          {/* Desktop Navigation */}
          <div className="hidden md:flex md:items-center md:gap-1">
            {navigation.map((item) => {
              const isActive =
                pathname === item.href ||
                (item.href !== "/" && pathname.startsWith(item.href));
              return item.isExternal ? (
                <a
                  key={item.name}
                  href={item.href}
                  target="_blank"
                  rel="noopener noreferrer"
                  className="relative inline-flex items-center gap-2 rounded-full px-4 py-2.5 text-sm font-medium text-muted-foreground transition-all duration-300 hover:bg-white/70 hover:text-foreground"
                >
                  <item.icon className="h-4 w-4" />
                  {t(item.name)}
                </a>
              ) : (
                <Link
                  key={item.name}
                  href={item.href}
                  className={cn(
                    "relative inline-flex items-center gap-2 rounded-full px-4 py-2.5 text-sm font-medium transition-all duration-300",
                    isActive
                      ? "bg-[var(--theme-surface-strong)] text-[var(--theme-primary)]"
                      : "text-muted-foreground hover:bg-white/70 hover:text-foreground",
                  )}
                >
                  <item.icon className="h-4 w-4" />
                  {t(item.name)}
                  {isActive && (
                    <span className="absolute inset-x-4 -bottom-0.5 h-0.5 rounded-full bg-gradient-to-r from-[var(--theme-secondary)] to-[var(--theme-accent)]" />
                  )}
                </Link>
              );
            })}
          </div>

          {/* Desktop Auth */}
          <div className="hidden md:flex md:items-center md:gap-3">
            <LanguageSwitcher />
            {isAuthenticated && user ? (
              <DropdownMenu>
                <DropdownMenuTrigger asChild>
                  <Button
                    variant="ghost"
                    className="flex items-center gap-2.5 rounded-full px-3 py-2 h-auto hover:bg-secondary/80"
                  >
                    <div className="flex h-9 w-9 items-center justify-center rounded-full bg-gradient-to-br from-[var(--theme-secondary)] to-[var(--theme-accent)] text-white text-sm font-semibold shadow-md">
                      {user.name.charAt(0).toUpperCase()}
                    </div>
                    <span className="text-sm font-medium">
                      {user.name.split(" ")[0]}
                    </span>
                    <ChevronDown className="h-4 w-4 text-muted-foreground" />
                  </Button>
                </DropdownMenuTrigger>
                <DropdownMenuContent
                  align="end"
                  className="w-56 rounded-2xl p-2 shadow-xl border-border/50"
                >
                  <div className="px-3 py-3 mb-1">
                    <p className="text-sm font-semibold">{user.name}</p>
                    <p className="text-xs text-muted-foreground mt-0.5">
                      {user.email}
                    </p>
                  </div>
                  <DropdownMenuSeparator className="bg-border/50" />
                  <DropdownMenuItem
                    asChild
                    className="rounded-xl cursor-pointer py-2.5 px-3 my-1"
                  >
                    <Link href={dashboardLink}>
                      <LayoutDashboard className="mr-3 h-4 w-4" />
                      {isAdmin ? t("common.adminPanel") : t("common.dashboard")}
                    </Link>
                  </DropdownMenuItem>
                  <DropdownMenuSeparator className="bg-border/50" />
                  <DropdownMenuItem
                    onClick={logout}
                    className="rounded-xl cursor-pointer text-destructive focus:text-destructive py-2.5 px-3"
                  >
                    <LogOut className="mr-3 h-4 w-4" />
                    {t("common.signOut")}
                  </DropdownMenuItem>
                </DropdownMenuContent>
              </DropdownMenu>
            ) : (
              <>
                <Link href="/auth/login">
                  <Button
                    variant="outline"
                    className="h-10 rounded-full border border-border/70 bg-white/80 px-5 font-medium text-foreground shadow-md backdrop-blur-md transition-all hover:-translate-y-0.5 hover:bg-white hover:text-[var(--theme-accent)] hover:shadow-lg"
                  >
                    {t("common.signIn")}
                  </Button>
                </Link>
                <Link href="/auth/register">
                  <Button className="rounded-full px-6 h-10">
                    {t("common.becomeGuide")}
                  </Button>
                </Link>
              </>
            )}
          </div>

          {/* Mobile Menu */}
          <Sheet open={isOpen} onOpenChange={setIsOpen}>
            <SheetTrigger asChild className="md:hidden">
              <Button
                variant="ghost"
                size="icon"
                className="rounded-full h-10 w-10"
              >
                <Menu className="h-5 w-5" />
                <span className="sr-only">{t("nav.openMenu")}</span>
              </Button>
            </SheetTrigger>
            <SheetContent side="right" className="w-80 p-0 border-l-border/50">
              <SheetTitle className="sr-only">
                {t("nav.navigationMenu")}
              </SheetTitle>
              <div className="flex flex-col h-full">
                <div className="p-6 border-b border-border/50">
                  <Link
                    href="/"
                    className="flex items-center gap-3"
                    onClick={() => setIsOpen(false)}
                  >
                    <Image
                      src="/uttarakhand-logo.png"
                      alt="Uttarakhand Tourism Guides"
                      width={180}
                      height={50}
                      className="h-12 w-auto object-contain"
                    />
                  </Link>
                </div>
                <nav className="flex-1 p-4">
                  <div className="space-y-1">
                    {navigation.map((item) => {
                      const isActive =
                        pathname === item.href ||
                        (item.href !== "/" && pathname.startsWith(item.href));
                      return item.isExternal ? (
                        <a
                          key={item.name}
                          href={item.href}
                          target="_blank"
                          rel="noopener noreferrer"
                          onClick={() => setIsOpen(false)}
                          className="flex items-center gap-3 rounded-xl px-4 py-3.5 text-sm font-medium text-muted-foreground transition-all hover:bg-secondary/80 hover:text-foreground"
                        >
                          <item.icon className="h-4 w-4 text-[var(--theme-primary)]" />
                          {t(item.name)}
                        </a>
                      ) : (
                        <Link
                          key={item.name}
                          href={item.href}
                          onClick={() => setIsOpen(false)}
                          className={cn(
                            "flex items-center gap-3 px-4 py-3.5 text-sm font-medium rounded-xl transition-all",
                            isActive
                              ? "bg-gradient-to-r from-[var(--theme-secondary)]/10 to-[var(--theme-accent)]/10 text-foreground"
                              : "text-muted-foreground hover:bg-secondary/80 hover:text-foreground",
                          )}
                        >
                          <item.icon className="h-4 w-4 text-[var(--theme-primary)]" />
                          {t(item.name)}
                        </Link>
                      );
                    })}
                  </div>
                </nav>
                <div className="p-4 border-t border-border/50 space-y-3">
                  <LanguageSwitcher />
                  {isAuthenticated && user ? (
                    <>
                      <div className="flex items-center gap-3 px-4 py-4 bg-secondary/50 rounded-2xl">
                        <div className="flex h-11 w-11 items-center justify-center rounded-full bg-gradient-to-br from-[var(--theme-secondary)] to-[var(--theme-accent)] text-white font-semibold shadow-md">
                          {user.name.charAt(0).toUpperCase()}
                        </div>
                        <div>
                          <p className="text-sm font-semibold">{user.name}</p>
                          <p className="text-xs text-muted-foreground">
                            {user.email}
                          </p>
                        </div>
                      </div>
                      <Link
                        href={dashboardLink}
                        onClick={() => setIsOpen(false)}
                      >
                        <Button
                          variant="outline"
                          className="w-full rounded-xl justify-start h-11 border-border/50"
                        >
                          <LayoutDashboard className="mr-3 h-4 w-4" />
                          {isAdmin
                            ? t("common.adminPanel")
                            : t("common.dashboard")}
                        </Button>
                      </Link>
                      <Button
                        variant="ghost"
                        onClick={() => {
                          logout();
                          setIsOpen(false);
                        }}
                        className="w-full rounded-xl justify-start h-11 text-destructive hover:text-destructive hover:bg-destructive/10"
                      >
                        <LogOut className="mr-3 h-4 w-4" />
                        {t("common.signOut")}
                      </Button>
                    </>
                  ) : (
                    <>
                      <Link href="/auth/login" onClick={() => setIsOpen(false)}>
                        <Button
                          variant="outline"
                          className="w-full rounded-xl h-11 border-border/50"
                        >
                          {t("common.signIn")}
                        </Button>
                      </Link>
                      <Link
                        href="/auth/register"
                        onClick={() => setIsOpen(false)}
                      >
                        <Button className="w-full rounded-xl h-11">
                          {t("common.becomeGuide")}
                        </Button>
                      </Link>
                    </>
                  )}
                </div>
              </div>
            </SheetContent>
          </Sheet>
        </div>
      </nav>
    </header>
  );
}
