"use client"

import { useAuth } from "@/contexts/auth-context"
import Header from "@/components/navigation/header"
import { usePathname } from "next/navigation"

interface AppLayoutProps {
  children: React.ReactNode
}

export function AppLayout({ children }: AppLayoutProps) {
  const { isAuthenticated, isLoading } = useAuth()
  const pathname = usePathname()
  
  // Don't show header on auth pages
  const isAuthPage = pathname === "/auth"
  
  // Show loading state while checking auth
  if (isLoading) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="animate-spin rounded-full h-32 w-32 border-b-2 border-blue-600"></div>
      </div>
    )
  }
  
  // For auth pages, don't show header
  if (isAuthPage) {
    return <>{children}</>
  }
  
  // For authenticated users on other pages, show header
  if (isAuthenticated) {
    return (
      <>
        <Header />
        {children}
      </>
    )
  }
  
  // For unauthenticated users on non-auth pages, redirect to auth
  return (
    <>
      <Header />
      {children}
    </>
  )
}
