// app/components/admin/Header.tsx
'use client';

import { useState } from 'react';
import { useRouter } from 'next/navigation';
import { useUserStore } from '@/stores/admin-store';
import { useTheme } from '@/providers/ThemeProvider';

interface HeaderProps {
  onMenuClick: () => void;
}

export default function Header({ onMenuClick }: HeaderProps) {
  const [isProfileOpen, setIsProfileOpen] = useState(false);
  const router = useRouter();
  const { theme, toggleTheme } = useTheme();
  const {user, clearUser} = useUserStore();

  const handleLogout = async () => {
    localStorage.removeItem('admin_user');
    await fetch('/api/backend/admin/auth/logout', { method: 'POST' });
    clearUser();
    router.push('/admin/login');
  };

  return (
    <header className="shrink-0 relative z-10">
      <div className="relative bg-var-topbar shadow-sm border-b border-var-topbar-border">
        <div className="flex justify-between items-center px-4 py-3 sm:px-6 lg:px-8">
          {/* Left side - Menu button for mobile */}
          <div className="flex items-center">
            <button
              type="button"
              className="lg:hidden p-2 rounded-md text-var-text-muted hover:text-var-text hover:bg-var-surface-hover focus:outline-none focus:ring-2 focus:ring-inset focus:ring-var-primary"
              onClick={onMenuClick}
            >
              <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h16" />
              </svg>
            </button>
            
          </div>

          {/* Right side - User menu */}
          <div className="flex items-center space-x-4">
            {/* Theme Toggle Button */}
            <button
              onClick={toggleTheme}
              className="p-2 text-var-text-muted hover:text-var-text hover:bg-var-surface-hover rounded-lg transition-colors"
              title={theme === 'light' ? 'Switch to Dark Mode' : 'Switch to Light Mode'}
            >
              {theme === 'light' ? (
                <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" />
                </svg>
              ) : (
                <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
                </svg>
              )}
            </button>

            {/* Notifications */}
            <button className="p-2 text-var-text-muted hover:text-var-text hover:bg-var-surface-hover rounded-lg transition-colors relative">
              <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 17h5l-5 5v-5zM10.5 3.75a6 6 0 0 0-6 6v2.25l-2.47 2.47a.75.75 0 0 0 .53 1.28h15.88a.75.75 0 0 0 .53-1.28L16.5 12V9.75a6 6 0 0 0-6-6z" />
              </svg>
              <span className="absolute top-1 right-1 w-2 h-2 bg-var-danger rounded-full"></span>
            </button>

            {/* Profile dropdown */}
            <div className="relative">
              <button
                onClick={() => setIsProfileOpen(!isProfileOpen)}
                className="flex items-center space-x-3 text-sm focus:outline-none"
              >
                <div className="w-8 h-8 bg-var-primary rounded-full flex items-center justify-center text-white text-sm font-bold">
                  {user?.first_name?.[0]}{user?.last_name?.[0]}
                </div>
                <div className="hidden md:block text-left">
                  <p className="font-medium text-var-text">{user?.first_name} {user?.last_name}</p>
                  <p className="text-xs text-var-text-muted">{user?.email}</p>
                </div>
                <svg className="w-4 h-4 text-var-text-muted" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
                </svg>
              </button>

              {/* Dropdown menu */}
              {isProfileOpen && (
                <div className="absolute right-0 mt-2 w-48 bg-var-surface rounded-lg shadow-var-modal border border-var-border py-1 z-50">
                  <a
                    href="#"
                    className="block px-4 py-2 text-sm text-var-text hover:bg-var-surface-hover transition-colors"
                  >
                    Your Profile
                  </a>
                  <a
                    href="#"
                    className="block px-4 py-2 text-sm text-var-text hover:bg-var-surface-hover transition-colors"
                  >
                    Settings
                  </a>
                  <div className="border-t border-var-border my-1"></div>
                  <button
                    onClick={handleLogout}
                    className="block w-full text-left px-4 py-2 text-sm text-var-danger hover:bg-var-surface-hover transition-colors"
                  >
                    Sign out
                  </button>
                </div>
              )}
            </div>
          </div>
        </div>
      </div>
    </header>
  );
}