import type { LucideIcon } from 'lucide-react';
import { TrendingUp, TrendingDown, Minus } from 'lucide-react';
import { formatCurrency } from '@/lib/utils';
import { cn } from '@/lib/utils';

interface StatCardProps {
  title: string;
  value: number;
  icon: LucideIcon;
  isCurrency?: boolean;
  trend?: string;
  compareValue?: number;
  compareLabel?: string;
  accentColor?: 'amber' | 'green' | 'blue' | 'purple' | 'red' | 'orange';
}

const accentMap = {
  amber: {
    iconBg: 'bg-gradient-to-br from-amber-100 to-amber-50',
    iconColor: 'text-amber-600',
    bar: 'from-amber-400 to-amber-300',
  },
  green: {
    iconBg: 'bg-gradient-to-br from-emerald-100 to-emerald-50',
    iconColor: 'text-emerald-600',
    bar: 'from-emerald-400 to-emerald-300',
  },
  blue: {
    iconBg: 'bg-gradient-to-br from-blue-100 to-blue-50',
    iconColor: 'text-blue-900',
    bar: 'from-blue-900 to-blue-800',
  },
  purple: {
    iconBg: 'bg-gradient-to-br from-violet-100 to-violet-50',
    iconColor: 'text-violet-600',
    bar: 'from-violet-400 to-violet-300',
  },
  red: {
    iconBg: 'bg-gradient-to-br from-red-100 to-red-50',
    iconColor: 'text-red-600',
    bar: 'from-red-400 to-red-300',
  },
  orange: {
    iconBg: 'bg-gradient-to-br from-orange-100 to-orange-50',
    iconColor: 'text-orange-600',
    bar: 'from-orange-400 to-orange-300',
  },
};

export default function StatCard({
  title,
  value,
  icon: Icon,
  isCurrency = false,
  trend,
  compareValue,
  compareLabel,
  accentColor = 'amber',
}: StatCardProps) {
  const accent = accentMap[accentColor];

  // Calculate comparison percentage
  let changePercent: number | null = null;
  let changeDirection: 'up' | 'down' | 'flat' = 'flat';
  if (compareValue !== undefined && compareValue > 0) {
    changePercent = Math.round(((value - compareValue) / compareValue) * 100);
    changeDirection = changePercent > 0 ? 'up' : changePercent < 0 ? 'down' : 'flat';
  } else if (compareValue !== undefined && compareValue === 0 && value > 0) {
    changeDirection = 'up';
    changePercent = 100;
  }

  return (
    <div className="group relative overflow-hidden rounded-2xl bg-white border border-border p-3.5 sm:p-5 lg:p-6 card-hover cursor-default" style={{ boxShadow: 'var(--shadow-card)' }}>
      {/* Top accent bar */}
      <div className={cn('absolute top-0 left-0 right-0 h-1 bg-gradient-to-r', accent.bar)} />

      <div className="flex items-start justify-between gap-2 sm:gap-3">
        <div className="space-y-1 sm:space-y-1.5 min-w-0 flex-1">
          <p className="text-[10px] sm:text-xs lg:text-sm font-medium text-brand-500 truncate">{title}</p>
          <p className="text-base sm:text-xl lg:text-2xl font-bold text-brand-700 font-heading tabular-nums truncate leading-tight">
            {isCurrency ? formatCurrency(value) : value.toLocaleString('id-ID')}
          </p>
          <div className="flex items-center gap-1.5 sm:gap-2 flex-wrap">
            {changePercent !== null && (
              <span className={cn(
                'inline-flex items-center gap-0.5 text-[10px] sm:text-[11px] font-semibold',
                changeDirection === 'up' && 'text-emerald-600',
                changeDirection === 'down' && 'text-red-500',
                changeDirection === 'flat' && 'text-brand-400',
              )}>
                {changeDirection === 'up' && <TrendingUp className="h-2.5 w-2.5 sm:h-3 sm:w-3" />}
                {changeDirection === 'down' && <TrendingDown className="h-2.5 w-2.5 sm:h-3 sm:w-3" />}
                {changeDirection === 'flat' && <Minus className="h-2.5 w-2.5 sm:h-3 sm:w-3" />}
                {changeDirection === 'up' ? '+' : ''}{changePercent}%
              </span>
            )}
            {(compareLabel || trend) && (
              <span className="text-[10px] sm:text-[11px] text-brand-400 font-medium truncate">{compareLabel || trend}</span>
            )}
          </div>
        </div>
        <div className={cn('flex h-9 w-9 sm:h-11 sm:w-11 lg:h-12 lg:w-12 shrink-0 items-center justify-center rounded-xl', accent.iconBg)}>
          <Icon className={cn('h-4 w-4 sm:h-5 sm:w-5 lg:h-6 lg:w-6', accent.iconColor)} />
        </div>
      </div>
    </div>
  );
}
