"use client";

import { useState, useEffect } from "react";
import { ChevronDown, LogOut, PanelLeft } from "lucide-react";
import * as DropdownMenu from "@radix-ui/react-dropdown-menu";
import { useAuth } from "@/contexts/AuthContext";
import { cn } from "@/lib/utils";

interface DashboardHeaderProps {
  toggleSidebar: () => void;
  isSidebarOpen: boolean;
}

export default function DashboardHeader({ toggleSidebar, isSidebarOpen }: DashboardHeaderProps) {
  const { user, logout } = useAuth();
  const [isMobile, setIsMobile] = useState(false);

  useEffect(() => {
    const check = () => setIsMobile(window.innerWidth < 768);
    check();
    window.addEventListener("resize", check);
    return () => window.removeEventListener("resize", check);
  }, []);

  const leftPx = isMobile ? 0 : isSidebarOpen ? 260 : 68;

  const initials = user
    ? `${user.firstName?.[0] ?? ""}${user.lastName?.[0] ?? ""}`.toUpperCase()
    : "?";

  return (
    <header
      style={{ left: leftPx }}
      className="fixed top-0 right-0 z-30 h-16 flex items-center justify-between px-6 bg-white border-b border-gray-200 transition-all duration-300"
    >
      {/* Left */}
      <button
        onClick={toggleSidebar}
        className="p-2 rounded-lg hover:bg-gray-100 transition-colors text-gray-500"
        aria-label="Toggle sidebar"
      >
        <PanelLeft size={18} />
      </button>

      {/* Right */}
      <DropdownMenu.Root>
        <DropdownMenu.Trigger asChild>
          <button className="flex items-center gap-2.5 px-3 py-2 rounded-lg hover:bg-gray-100 transition-colors outline-none">
            <div className="w-7 h-7 rounded-lg bg-rose-600 flex items-center justify-center text-white text-xs font-semibold flex-shrink-0">
              {initials}
            </div>
            <div className="hidden md:block text-left">
              <p className="text-[13px] font-medium text-gray-900 leading-tight">
                {user?.firstName} {user?.lastName}
              </p>
              <p className="text-[11px] text-gray-400 truncate max-w-[160px]">{user?.email}</p>
            </div>
            <ChevronDown size={14} className="text-gray-400 hidden md:block" />
          </button>
        </DropdownMenu.Trigger>

        <DropdownMenu.Portal>
          <DropdownMenu.Content
            className="min-w-[220px] bg-white rounded-xl shadow-lg border border-gray-200 p-1.5 z-50"
            align="end"
            sideOffset={8}
          >
            <div className="px-3 py-2 mb-1">
              <p className="text-[13px] font-medium text-gray-900">
                {user?.firstName} {user?.lastName}
              </p>
              <p className="text-[11px] text-gray-400 truncate">{user?.email}</p>
              <span className="inline-block mt-1.5 text-[10px] font-medium px-2 py-0.5 rounded-full bg-rose-50 text-rose-700 border border-rose-100">
                Admin Général
              </span>
            </div>

            <div className="h-px bg-gray-100 my-1" />

            <DropdownMenu.Item
              className={cn(
                "flex items-center gap-2.5 px-3 py-2 rounded-lg text-[13px] font-medium",
                "text-red-600 cursor-pointer outline-none hover:bg-red-50 transition-colors"
              )}
              onClick={logout}
            >
              <LogOut size={14} />
              Déconnexion
            </DropdownMenu.Item>
          </DropdownMenu.Content>
        </DropdownMenu.Portal>
      </DropdownMenu.Root>
    </header>
  );
}
