import { Search, X, Command } from 'lucide-react';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';

interface SearchBarProps {
  value: string;
  onChange: (value: string) => void;
  placeholder?: string;
  className?: string;
  compact?: boolean;
}

export function SearchBar({
  value,
  onChange,
  placeholder = 'Search skills, aliases, keywords…',
  className,
  compact = false,
}: SearchBarProps) {
  return (
    <div className={cn('relative group', className)}>
      <Search
        className={cn(
          'absolute top-1/2 -translate-y-1/2 text-muted-foreground transition-colors group-focus-within:text-primary pointer-events-none',
          compact ? 'left-2.5 h-3.5 w-3.5' : 'left-3.5 h-4 w-4'
        )}
      />
      <Input
        type="search"
        value={value}
        onChange={(e) => onChange(e.target.value)}
        placeholder={placeholder}
        className={cn(
          'pl-10 pr-20 bg-background/80 border-border/60 rounded-lg shadow-sm focus:border-primary/50 focus:ring-2 focus:ring-primary/20 transition-all duration-200',
          compact ? 'h-8 text-sm pl-9 pr-16' : 'h-11 rounded-xl'
        )}
        aria-label="Search skills"
      />
      {!value && (
        <kbd className="hidden sm:inline-flex absolute right-3 top-1/2 -translate-y-1/2 items-center gap-0.5 rounded-md border border-border/60 bg-muted/50 px-1.5 py-0.5 text-[10px] font-mono text-muted-foreground pointer-events-none">
          <Command className="h-2.5 w-2.5" />
          K
        </kbd>
      )}
      {value && (
        <Button
          variant="ghost"
          size="icon"
          className={cn(
            'absolute right-0.5 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground cursor-pointer',
            compact ? 'h-7 w-7' : 'h-8 w-8'
          )}
          onClick={() => onChange('')}
          aria-label="Clear search"
        >
          <X className="h-4 w-4" />
        </Button>
      )}
    </div>
  );
}