import { useAuth } from '@/lib/auth'
import { useNavigate } from 'react-router-dom'

interface HeaderProps {
  onMenuToggle: () => void
  title?: string
  subtitle?: string
}

export default function Header({ onMenuToggle, title = 'Tableau de bord', subtitle }: HeaderProps) {
  const { user, logout } = useAuth()
  const navigate = useNavigate()

  const handleLogout = () => {
    logout()
    navigate('/login')
  }

  const initials = user?.role === 'ADMIN' ? 'AD' : 'DR'

  return (
    <header className="flex items-center justify-between px-6 lg:px-8 py-4 bg-card border-b border-border">
      {/* Left: hamburger + title */}
      <div className="flex items-center gap-4">
        <button
          onClick={onMenuToggle}
          className="lg:hidden p-2 rounded-lg hover:bg-bg transition-colors"
          aria-label="Menu"
        >
          <svg width="20" height="20" viewBox="0 0 20 20" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round">
            <path d="M3 5h14M3 10h14M3 15h14" />
          </svg>
        </button>
        <div>
          <h1 className="text-xl lg:text-2xl font-bold text-ink m-0">{title}</h1>
          {subtitle && (
            <p className="text-sm text-muted m-0 hidden sm:block">{subtitle}</p>
          )}
        </div>
      </div>

      {/* Right: notifications + profile */}
      <div className="flex items-center gap-4">
        {/* Notifications */}
        <button className="relative p-2 rounded-lg hover:bg-bg transition-colors" aria-label="Notifications">
          <svg width="20" height="20" viewBox="0 0 20 20" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
            <path d="M10 2a5 5 0 00-5 5v3l-1.5 2.5h13L15 10V7a5 5 0 00-5-5z" />
            <path d="M8 15a2 2 0 004 0" />
          </svg>
          <span className="absolute top-1.5 right-1.5 w-2 h-2 bg-accent rounded-full" />
        </button>

        {/* Profile */}
        <div className="hidden sm:flex items-center gap-3">
          <div className="w-9 h-9 rounded-full bg-accent/10 flex items-center justify-center">
            <span className="text-accent font-semibold text-sm">{initials}</span>
          </div>
          <div className="hidden lg:block">
            <div className="text-sm font-medium text-ink">{user?.role === 'ADMIN' ? 'Administrateur' : 'Directeur'}</div>
          </div>
        </div>

        {/* Logout */}
        <button
          onClick={handleLogout}
          className="p-2 rounded-lg hover:bg-bg transition-colors text-muted hover:text-ink"
          title="Deconnexion"
        >
          <svg width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
            <path d="M6 15H3a1 1 0 01-1-1V4a1 1 0 011-1h3M12 12l3-3-3-3M7 9h8" />
          </svg>
        </button>
      </div>
    </header>
  )
}
