import { cn } from '@/lib/utils'

interface TagBadgesProps {
  tags: string[]
  maxVisible?: number
  onTagClick?: (tag: string) => void
  size?: 'sm' | 'md'
  variant?: 'default' | 'muted'
}

export function TagBadges({
  tags,
  maxVisible = 3,
  onTagClick,
  size = 'sm',
  variant = 'default',
}: TagBadgesProps) {
  if (!tags || tags.length === 0) return null

  const visibleTags = tags.slice(0, maxVisible)
  const hiddenCount = tags.length - maxVisible

  return (
    <div className="flex flex-wrap items-center gap-1">
      {visibleTags.map((tag) => (
        <button
          key={tag}
          type="button"
          onClick={(e) => {
            if (onTagClick) {
              e.stopPropagation()
              onTagClick(tag)
            }
          }}
          disabled={!onTagClick}
          className={cn(
            'rounded-full font-label transition-colors',
            size === 'sm' ? 'px-2 py-0.5 text-xs' : 'px-2.5 py-1 text-sm',
            variant === 'default'
              ? 'bg-accent text-muted-foreground hover:bg-accent'
              : 'bg-accent/50 text-muted-foreground hover:bg-accent',
            !onTagClick && 'cursor-default'
          )}
        >
          {tag}
        </button>
      ))}
      {hiddenCount > 0 && (
        <span
          className={cn(
            'rounded-full font-label',
            size === 'sm' ? 'px-2 py-0.5 text-xs' : 'px-2.5 py-1 text-sm',
            'bg-accent/50 text-muted-foreground'
          )}
          title={tags.slice(maxVisible).join(', ')}
        >
          +{hiddenCount} more
        </span>
      )}
    </div>
  )
}

export default TagBadges
