'use client';

import * as React from 'react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { Menu, Search, Bell, ChevronRight, Moon, Sun, X } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { useTheme } from '@/components/providers/ThemeProvider';
import { cn } from '@/lib/utils/cn';
import { useSidebar } from './SidebarProvider';
import Logo from '@/components/Logo';

export function AppHeader() {
  const pathname = usePathname();
  const { toggle, isOpen } = useSidebar();
  
  // Custom breadcrumb logic
  const pathSegments = pathname.split('/').filter(Boolean);
  
  return (
    <header className="h-16 border-b border-border bg-background/10 backdrop-blur-xl sticky top-0 z-40 flex items-center justify-between px-4 lg:px-8">
      <div className="flex items-center gap-4">
        <Button 
          variant="ghost" 
          size="icon" 
          className="lg:hidden text-muted-foreground hover:text-foreground"
          onClick={toggle}
        >
          {isOpen ? <X className="h-5 w-5" /> : <Menu className="h-5 w-5" />}
        </Button>
        
        <nav className="flex items-center text-sm font-medium">
          <Link href="/" className="flex items-center gap-2 group">
            <Logo size={20} className="md:hidden" />
            <span className="font-bold bg-clip-text text-transparent bg-gradient-to-r from-purple-400 to-pink-400 group-hover:opacity-80 transition-opacity">
              Kit
            </span>
          </Link>
          {pathSegments.map((segment, index) => {
            const href = `/${pathSegments.slice(0, index + 1).join('/')}`;
            const isLast = index === pathSegments.length - 1;
            
            return (
              <React.Fragment key={href}>
                <ChevronRight className="h-4 w-4 mx-1 text-muted-foreground/30" />
                <Link 
                  href={href}
                  className={cn(
                    "capitalize transition-colors",
                    isLast ? "text-foreground font-semibold" : "text-muted-foreground hover:text-foreground font-medium"
                  )}
                >
                  {segment.replace(/-/g, ' ')}
                </Link>
              </React.Fragment>
            );
          })}
        </nav>
      </div>

      <div className="flex items-center gap-2 sm:gap-4">
        <ThemeToggleComponent />
      </div>
    </header>
  );
}

function ThemeToggleComponent() {
  const { resolvedTheme, setTheme } = useTheme();
  
  return (
    <Button 
      variant="ghost" 
      size="icon"
      onClick={() => setTheme(resolvedTheme === 'dark' ? 'light' : 'dark')}
      className="text-muted-foreground hover:text-foreground hover:bg-accent/50"
      title={`Switch to ${resolvedTheme === 'dark' ? 'light' : 'dark'} mode`}
    >
      {resolvedTheme === 'dark' ? (
        <Sun className="h-5 w-5" />
      ) : (
        <Moon className="h-5 w-5" />
      )}
    </Button>
  );
}
