"use client";

import { usePathname } from "next/navigation";
import { useAuth } from "@/contexts/AuthContext";
import Header from "@/components/Header";
import Footer from "@/components/Footer";
import CookieConsent from "@/components/CookieConsent";

export default function LayoutClient({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();
  const { user } = useAuth();

  // Ne pas afficher Header/Footer dans l'espace admin et employé
  const isAdminRoute = pathname?.startsWith("/admin");
  const isEmployeRoute = pathname?.startsWith("/employe");
  const isHomePage = pathname === "/";
  const isLoginPage = pathname === "/login" || pathname === "/register";

  // Vérifier si l'utilisateur est admin ou employé
  const userRole = user?.role?.toUpperCase();
  const isAdminOrEmployee = userRole === "ADMIN" || userRole === "EMPLOYEE";

  // Ne pas afficher le header si:
  // 1. On est sur une route admin/employé
  // 2. On est sur login/register ET l'utilisateur est admin/employé (en cours de redirection)
  if (isAdminRoute || isEmployeRoute || (isLoginPage && isAdminOrEmployee)) {
    return <>{children}</>;
  }

  return (
    <>
      <Header />
      <main className={isHomePage ? "flex-1" : "flex-1 container mx-auto px-4 py-6 sm:px-6 lg:px-8"}>
        {children}
      </main>
      <Footer />
      <CookieConsent />
    </>
  );
}
