<template>
  <div class="bg-[var(--brand-surface)] rounded-lg p-4 border-l-4" :class="borderColor">
    <h3 class="text-lg font-semibold mb-3 capitalize text-[var(--brand-text)]">{{ title }}</h3>
    <div class="space-y-2">
      <div class="flex justify-between items-center">
        <span class="text-[var(--brand-text-muted)] text-sm">Total {{ unidad }}:</span>
        <span class="font-medium text-[var(--brand-text)]"><span class="font-bold">{{ formatNumber(metrics.value.totalCantidad) }}</span> <span class="font-bold">{{ unidad }}</span></span>
      </div>
      <div class="flex justify-between items-center">
        <span class="text-[var(--brand-text-muted)] text-sm">Precio promedio:</span>
        <span class="font-medium text-[var(--brand-text)]">{{ formatCurrency(metrics.value.precioPromedio) }}</span>
      </div>
      <div class="flex justify-between items-center pt-2 border-t border-[var(--brand-border)]">
        <span class="text-[var(--brand-text)] font-semibold">Total cobrado:</span>
        <span class="font-bold text-white-400">{{ formatCurrency(metrics.value.totalCobrado) }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { RechazoMetrics } from '~/composables/useRechazosMetrics'

interface Props {
  title: string
  metrics: ComputedRef<RechazoMetrics>
  unidad: 'libras' | 'galones'
  color?: 'blue' | 'green' | 'yellow' | 'red' | 'purple' | 'pink'
}

const props = withDefaults(defineProps<Props>(), {
  color: 'blue'
})

const borderColor = computed(() => {
  const colors = {
    blue: 'border-blue-500',
    green: 'border-green-500',
    yellow: 'border-yellow-500',
    red: 'border-red-500',
    purple: 'border-purple-500',
    pink: 'border-pink-500'
  }
  return colors[props.color]
})

const formatNumber = (value: number) => {
  return value.toLocaleString('en-US', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  })
}

const formatCurrency = (value: number) => {
  return new Intl.NumberFormat('es-HN', {
    style: 'currency',
    currency: 'HNL',
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  }).format(value).replace('HNL', 'L')
}
</script>