import { Shield, ArrowLeft, Settings, Menu } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { RefreshButton } from '@/components/ui/refresh-button';
import { Link, useLocation } from 'react-router-dom';
import { ThemeToggle } from '@/components/theme/ThemeToggle';
import { AuthButtons } from '@/components/auth/AuthButtons';
import { NotificationCenter } from '@/components/notifications/NotificationCenter';
import { useUserRole } from '@/hooks/useUserRole';
import { useAuth } from '@/hooks/useAuth';
import { useIsMobile } from '@/hooks/use-mobile';
import {
  Sheet,
  SheetContent,
  SheetHeader,
  SheetTitle,
  SheetTrigger,
} from '@/components/ui/sheet';

export function AdminHeader({ onRefresh, isRefreshing }: { onRefresh?: () => void; isRefreshing?: boolean }) {
  const { permissions } = useUserRole();
  const { user } = useAuth();
  const location = useLocation();
  const isMobile = useIsMobile();
  
  const isSettingsPage = location.pathname === '/admin/settings';
  const backLink = isSettingsPage ? '/admin' : '/';
  const backText = isSettingsPage ? 'Back to Admin' : 'Back to ThrillWiki';
  const pageTitle = isSettingsPage ? 'Admin Settings' : 'Admin Dashboard';
  
  return (
    <header className="sticky top-0 z-50 w-full border-b border-border bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
      <div className="container flex h-16 items-center justify-between px-4">
        {/* Left Section - Navigation */}
        <div className="flex items-center gap-4">
          <Button variant="ghost" size="sm" asChild>
            <Link to={backLink} className="flex items-center gap-2">
              <ArrowLeft className="w-4 h-4" />
              <span className="hidden sm:inline">{backText}</span>
            </Link>
          </Button>
          
          <div className="h-6 w-px bg-border hidden sm:block" />
          
          <div className="flex items-center gap-2">
            <Shield className="w-6 h-6 text-primary" />
            <h1 className="text-lg font-semibold">
              <span className="sm:hidden">Admin</span>
              <span className="hidden sm:inline">{pageTitle}</span>
            </h1>
          </div>
        </div>

        {/* Right Section - Admin actions */}
        <div className="flex items-center gap-2">
          {/* Mobile Menu */}
          <Sheet>
            <SheetTrigger asChild className="md:hidden">
              <Button variant="ghost" size="icon">
                <Menu className="h-5 w-5" />
                <span className="sr-only">Open menu</span>
              </Button>
            </SheetTrigger>
            <SheetContent side="right" className="w-[300px] sm:w-[400px]">
              <SheetHeader>
                <SheetTitle>Admin Menu</SheetTitle>
              </SheetHeader>
              <div className="flex flex-col gap-4 mt-6">
                <div className="flex items-center justify-between">
                  <span className="text-sm font-medium">Theme</span>
                  <ThemeToggle />
                </div>
                <RefreshButton
                  onRefresh={onRefresh!}
                  isLoading={isRefreshing}
                  variant="ghost"
                  className="justify-start w-full"
                />
                {permissions?.role_level === 'superuser' && !isSettingsPage && (
                  <Button variant="ghost" asChild className="justify-start">
                    <Link to="/admin/settings">
                      <Settings className="w-4 h-4 mr-2" />
                      Settings
                    </Link>
                  </Button>
                )}
              </div>
            </SheetContent>
          </Sheet>

          {/* Desktop Actions */}
          {onRefresh && (
            <RefreshButton
              onRefresh={onRefresh}
              isLoading={isRefreshing}
              variant="ghost"
              size="sm"
              className="hidden md:flex"
            />
          )}
          {permissions?.role_level === 'superuser' && !isSettingsPage && (
            <Button variant="ghost" size="sm" asChild className="hidden md:flex">
              <Link to="/admin/settings">
                <Settings className="w-4 h-4" />
                <span className="hidden sm:ml-2 sm:inline">Settings</span>
              </Link>
            </Button>
          )}
          <div className="hidden md:block">
            <ThemeToggle />
          </div>
          {user && <NotificationCenter />}
          <AuthButtons />
        </div>
      </div>
    </header>
  );
}