import React, { useState } from 'react'
import { Button } from 'flowbite-react'
import { LuMinus, LuPlus } from 'react-icons/lu'
import buttonTheme from '../themes/button'

interface CounterProps {
  value?: number
  onChange?: (value: number) => void
  min?: number
  max?: number
  step?: number
  disabled?: boolean
  className?: string
}

const Counter: React.FC<CounterProps> = ({
  value = 0,
  onChange,
  min = 0,
  max = 100,
  step = 1,
  disabled = false,
  className = '',
}) => {
  const [count, setCount] = useState(value)

  const handleIncrement = () => {
    if (count < max) {
      const newValue = count + step
      setCount(newValue)
      onChange?.(newValue)
    }
  }

  const handleDecrement = () => {
    if (count > min) {
      const newValue = count - step
      setCount(newValue)
      onChange?.(newValue)
    }
  }

  return (
    <div className={`flex items-center ${className} h-14`}>
      <Button
        color="alternative"
        size="sm"
        theme={buttonTheme.button}
        onClick={handleDecrement}
        disabled={disabled || count <= min}
        className="px-4 py-1 rounded-l-lg h-12 rounded-r-none"
      >
        <LuMinus className="w-4 h-4" />
      </Button>

      <div className="h-12 flex-1 text-center border-slate-300 dark:border-gray-600 py-2 px-3 bg-slate-50 dark:bg-gray-700 text-sm font-medium flex items-center justify-center border-t border-b">
        {count}
      </div>

      <Button
        color="alternative"
        size="sm"
        theme={buttonTheme.button}
        onClick={handleIncrement}
        disabled={disabled || count >= max}
        className="px-4 py-1 rounded-r-lg h-12 rounded-l-none"
      >
        <LuPlus className="w-4 h-4" />
      </Button>
    </div>
  )
}

export default Counter
