import React from 'react'
import { useStepperContext } from './Index'
import { cn } from '@/lib/utils/tw-merge'
import { LuCheck } from 'react-icons/lu'

// Indicator component
interface IndicatorsProps {
  className?: string
  labels?: string[]
  icons?: React.ReactNode[]
}

const Indicators = ({
  className,
  labels = [],
  icons = [],
}: IndicatorsProps) => {
  const { currentStep, totalSteps, isMobile } = useStepperContext()

  return (
    <div
      className={cn(
        'relative',
        isMobile
          ? 'flex flex-col items-center justify-start space-y-4'
          : 'flex flex-row items-center justify-between',
        className,
      )}
    >
      {/* Track line - horizontal on desktop, vertical on mobile */}
      <div
        className={cn(
          'absolute bg-gray-200 rounded-full z-1',
          isMobile
            ? 'left-4 w-1'
            : 'left-0 w-full top-1/2 transform -translate-y-1/2 h-1',
        )}
        style={
          isMobile
            ? {
                top: '1rem', // Start from center of first icon (h-8 = 2rem, so center is 1rem)
                height: `calc(100% - 2rem)`, // End at center of last icon
              }
            : undefined
        }
      />

      {/* Progress Line - horizontal on desktop, vertical on mobile */}
      <div
        className={cn(
          'absolute rounded-full bg-blue-800 transition-all duration-300 ease-in-out z-1',
          isMobile
            ? 'left-4 w-1'
            : 'left-0 h-1 top-1/2 transform -translate-y-1/2',
        )}
        style={
          isMobile
            ? {
                top: '1rem', // Start from center of first icon
                height: `${(currentStep / (totalSteps - 1)) * (100 - (2 / totalSteps) * 100)}%`,
              }
            : { width: `${(currentStep / (totalSteps - 1)) * 100}%` }
        }
      />

      {Array.from({ length: totalSteps }).map((_, index) => {
        const status =
          index < currentStep
            ? 'completed'
            : index === currentStep
              ? 'current'
              : 'upcoming'

        return (
          <div
            key={index}
            className={cn(
              'relative z-10',
              isMobile
                ? 'flex flex-row items-center w-full'
                : 'flex flex-col items-center',
            )}
          >
            {/* Step icon/number */}
            <div
              className={cn(
                'flex items-center justify-center rounded-full border-2 text-sm font-medium cursor-pointer transition-colors duration-200 flex-shrink-0',
                isMobile ? 'h-8 w-8' : 'h-10 w-10',
                status === 'current' &&
                  'bg-blue-800 border-blue-800 text-white',
                status === 'completed' &&
                  'border-blue-800 bg-blue-800 text-white',
                status === 'upcoming' &&
                  'border-gray-300 bg-gray-100 text-gray-500',
              )}
            >
              {status === 'completed' ? (
                <LuCheck className={cn(isMobile ? 'h-4 w-4' : 'h-5 w-5')} />
              ) : (
                <>{icons[index] || index + 1}</>
              )}
            </div>

            {/* Label if provided */}
            {labels[index] && (
              <div
                className={cn(
                  isMobile ? 'ml-3 flex-1 text-left' : 'mt-2 text-center',
                )}
              >
                <div
                  className={cn(
                    'font-medium',
                    isMobile ? 'text-sm' : 'text-sm',
                    status === 'current'
                      ? 'font-medium text-blue-800'
                      : 'text-slate-500',
                  )}
                >
                  {labels[index]}
                </div>
              </div>
            )}
          </div>
        )
      })}
    </div>
  )
}

Indicators.displayName = 'Stepper.Indicators'

export default Indicators
