// Top Bar Component
import { useAuthStore } from '@/stores/authStore';
import { useNavigate } from 'react-router-dom';
import Button from '@/components/shared/Button';
import { runtimePortal } from '@/config/runtimePortal';
import './TopBar.css';

interface TopBarProps {
  sidebarCollapsed?: boolean;
  onToggleSidebar?: () => void;
}

export default function TopBar({ sidebarCollapsed = false, onToggleSidebar }: TopBarProps) {
  const { user, logout } = useAuthStore();
  const navigate = useNavigate();

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

  return (
    <header className="topbar">
      <div className="topbar__left">
        {onToggleSidebar && sidebarCollapsed && (
          <button
            type="button"
            className="topbar__menu-toggle"
            onClick={onToggleSidebar}
            aria-label="Open navigation panel"
            aria-expanded={!sidebarCollapsed}
          >
            ☰
          </button>
        )}
        <h1 className="topbar__title">{runtimePortal.topbarTitle}</h1>
      </div>
      <div className="topbar__right">
        <div className="topbar__user">
          <span className="topbar__user-name">{user?.name || 'User'}</span>
          <span className="topbar__user-role">{user?.role || ''}</span>
        </div>
        <Button variant="ghost" size="small" onClick={handleLogout}>
          Logout
        </Button>
      </div>
    </header>
  );
}
