"use client";

import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { Button } from "@/components/ui/button";
import { ThemeToggle } from "@/components/layout/theme-toggle";
import {
  BarChart3,
  User,
  Settings,
  Building2,
  ChevronDown,
  Users,
} from "lucide-react";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { authClient } from "@/clients";
import { useToast } from "@/hooks/use-toast";

interface NavigationProps {
  userInfo?: {
    firstName: string;
    lastName: string;
    teamName: string;
  };
}

export function Navigation({ userInfo }: NavigationProps = {}) {
  const pathname = usePathname();
  const router = useRouter();
  const { toast } = useToast();

  const handleLogout = async () => {
    try {
      await authClient.logout();
      toast({
        title: "Déconnexion réussie",
        description: "Vous avez été déconnecté avec succès.",
      });
      // Rediriger vers la page de login après déconnexion
      router.push("/login");
    } catch (error: any) {
      console.error("Logout failed:", error);
      toast({
        title: "Erreur de déconnexion",
        description: "Erreur lors de la déconnexion. Veuillez réessayer.",
        variant: "destructive",
      });
    }
  };

  const navItems = [
    {
      href: "/",
      label: "Tableau de bord",
      icon: BarChart3,
    },
    {
      href: "/evaluation",
      label: "Évaluation",
      icon: User,
    },
    {
      href: "/team",
      label: "Mon équipe",
      icon: Users,
    },
    {
      href: "/admin",
      label: "Administration",
      icon: Building2,
    },
  ];

  return (
    <header className="border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
      <div className="container mx-auto px-4 h-16 flex items-center justify-between">
        <div className="flex items-center space-x-6">
          <Link href="/" className="font-bold text-xl">
            PeakSkills
          </Link>

          <nav className="hidden md:flex items-center space-x-4">
            {navItems.map((item) => {
              const Icon = item.icon;
              const isActive = pathname === item.href;

              return (
                <Button
                  key={item.href}
                  variant={isActive ? "default" : "ghost"}
                  asChild
                  size="sm"
                >
                  <Link href={item.href} className="flex items-center gap-2">
                    <Icon className="h-4 w-4" />
                    {item.label}
                  </Link>
                </Button>
              );
            })}
          </nav>
        </div>

        <div className="flex items-center space-x-4">
          {userInfo && (
            <DropdownMenu>
              <DropdownMenuTrigger asChild>
                <Button
                  variant="ghost"
                  className="flex items-center gap-3 px-3 py-2 rounded-lg bg-muted/50 hover:bg-muted/70 transition-colors"
                >
                  <div className="w-8 h-8 rounded-full bg-primary/20 border border-primary/30 flex items-center justify-center">
                    <User className="h-4 w-4 text-primary" />
                  </div>
                  <div className="hidden sm:block text-left">
                    <p className="text-sm font-medium">
                      {userInfo.firstName} {userInfo.lastName}
                    </p>
                    <p className="text-xs text-muted-foreground">
                      {userInfo.teamName}
                    </p>
                  </div>
                  <ChevronDown className="h-4 w-4" />
                </Button>
              </DropdownMenuTrigger>
              <DropdownMenuContent align="end" className="w-56">
                <DropdownMenuItem asChild>
                  <Link href="/account" className="flex items-center gap-2">
                    <Settings className="h-4 w-4" />
                    Mon compte
                  </Link>
                </DropdownMenuItem>
                <DropdownMenuItem
                  onClick={handleLogout}
                  className="flex items-center gap-2"
                >
                  <User className="h-4 w-4" />
                  Se déconnecter
                </DropdownMenuItem>
              </DropdownMenuContent>
            </DropdownMenu>
          )}
          <ThemeToggle />
        </div>
      </div>
    </header>
  );
}
