import * as React from 'react'
import { cn } from '../core/cn'

type KPICardSize = 'sm' | 'default' | 'lg'

interface Delta {
  value: number
  isPositive: boolean
}

interface KPICardProps extends React.HTMLAttributes<HTMLDivElement> {
  label: string
  value: string | number
  delta?: Delta
  icon?: React.ReactNode
  subtitle?: string
  size?: KPICardSize
}

const sizeStyles: Record<KPICardSize, { value: string; label: string }> = {
  sm: { value: 'text-2xl font-bold', label: 'text-xs' },
  default: { value: 'text-3xl font-bold', label: 'text-sm' },
  lg: { value: 'text-4xl font-bold', label: 'text-base' },
}

const KPICard = React.forwardRef<HTMLDivElement, KPICardProps>(
  ({ label, value, delta, icon, subtitle, size = 'default', className, ...props }, ref) => {
    const styles = sizeStyles[size]
    const deltaColor = delta
      ? delta.value === 0 ? 'text-muted-foreground'
        : delta.isPositive ? 'text-green-600 dark:text-green-500'
        : 'text-red-600 dark:text-red-500'
      : ''

    return (
      <div ref={ref} className={cn('rounded-lg border bg-card p-4 text-card-foreground shadow-sm', className)} {...props}>
        <div className="flex items-start justify-between">
          <div className="space-y-1">
            <p className={cn('text-muted-foreground', styles.label)}>{label}</p>
            {subtitle && <p className="text-xs text-muted-foreground/80">{subtitle}</p>}
          </div>
          {icon && <div className="text-muted-foreground">{icon}</div>}
        </div>
        <div className="mt-3 flex items-end justify-between gap-4">
          <div className="space-y-1">
            <p className={cn('tracking-tight', styles.value)}>{value}</p>
            {delta && (
              <div className={cn('flex items-center gap-1 text-sm font-medium', deltaColor)}>
                <span>{delta.value > 0 ? '+' : ''}{delta.value}%</span>
              </div>
            )}
          </div>
        </div>
      </div>
    )
  }
)
KPICard.displayName = 'KPICard'

export { KPICard, type KPICardProps, type Delta, type KPICardSize }
