<template>
  <UCard class="brand-card border border-transparent">
    <template #header>
      <h2 class="text-xl font-bold brand-section-title">Totales qq Seco de Ingreso y Compra</h2>
    </template>

    <div class="flex flex-col gap-6">
      <!-- Sección: Totales Generales (Pagado + Pendiente) -->
      <div>
        <h3 class="text-sm font-semibold text-[var(--brand-text-muted)] uppercase tracking-wide mb-3">
          Totales Generales (Pagado + Pendiente)
        </h3>
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
          <div class="p-4 rounded-lg border transition-all bg-[#1c140c] border-red-600/40 text-red-400">
            <div class="flex items-center justify-between mb-1">
              <span class="text-xs uppercase tracking-wide opacity-80">Total Uva Ingresada</span>
              <UButton
                :label="unitDisplayLabel"
                size="xs"
                color="neutral"
                variant="ghost"
                @click="cycleUnitDisplay"
              />
            </div>
            <div>
              <span class="text-2xl font-bold">{{ formatUvaValueNumber(metrics.totalLbUvaIngresada.value, metrics.totalQqSecoUvaIngresado.value) }}</span>
              <span class="text-sm font-bold opacity-70 ml-2">{{ formatUvaValueUnit() }}</span>
            </div>
          </div>
          <MetricCard
            label="Total qq Seco Mojado"
            :value="formatNumber(metrics.totalQqSecoMojadoIngresado.value)"
            unit="qq"
            variant="info"
          />
          <MetricCard
            label="Total qq Seco Oreado"
            :value="formatNumber(metrics.totalQqSecoOreadoIngresado.value)"
            unit="qq"
            variant="warning"
          />
          <MetricCard
            label="Total qq Seco Ingresado"
            :value="formatNumber(metrics.totalQqSecoIngresado.value)"
            unit="qq"
            variant="primary"
          />
        </div>
      </div>

      <!-- Sección: Solo Pagados -->
      <div>
        <h3 class="text-sm font-semibold text-[var(--brand-text-muted)] uppercase tracking-wide mb-3">
          Solo Pagados
        </h3>
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
          <div class="p-4 rounded-lg border transition-all bg-[#1c140c] border-red-600/40 text-red-400">
            <div class="flex items-center justify-between mb-1">
              <span class="text-xs uppercase tracking-wide opacity-80">Total Uva Pagada</span>
              <UButton
                :label="unitDisplayLabel"
                size="xs"
                color="neutral"
                variant="ghost"
                @click="cycleUnitDisplay"
              />
            </div>
            <div>
              <span class="text-2xl font-bold">{{ formatUvaValueNumber(metrics.totalLbUvaPagada.value, metrics.totalQqSecoUvaPagado.value) }}</span>
              <span class="text-sm font-bold opacity-70 ml-2">{{ formatUvaValueUnit() }}</span>
            </div>
          </div>
          <MetricCard
            label="Total qq Seco Mojado Pagado"
            :value="formatNumber(metrics.totalQqSecoMojadoPagado.value)"
            unit="qq"
            variant="info"
          />
          <MetricCard
            label="Total qq Seco Oreado Pagado"
            :value="formatNumber(metrics.totalQqSecoOreadoPagado.value)"
            unit="qq"
            variant="warning"
          />
          <MetricCard
            label="Total qq Seco Comprado"
            :value="formatNumber(metrics.totalQqSecoComprado.value)"
            unit="qq"
            variant="primary"
          />
        </div>
      </div>

      <!-- Sección: Inventario en Depósito -->
      <div>
        <h3 class="text-sm font-semibold text-[var(--brand-text-muted)] uppercase tracking-wide mb-3">
          Inventario en Depósito
        </h3>
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
          <div class="p-4 rounded-lg border transition-all bg-[#1c140c] border-red-600/40 text-red-400">
            <div class="flex items-center justify-between mb-1">
              <span class="text-xs uppercase tracking-wide opacity-80">Total Uva en Depósito</span>
              <UButton
                :label="unitDisplayLabel"
                size="xs"
                color="neutral"
                variant="ghost"
                @click="cycleUnitDisplay"
              />
            </div>
            <div>
              <span class="text-2xl font-bold">{{ formatUvaValueNumber(metrics.totalLbUvaDeposito.value, 0) }}</span>
              <span class="text-sm font-bold opacity-70 ml-2">{{ formatUvaValueUnit() }}</span>
            </div>
          </div>
          <MetricCard
            label="Total qq Seco Mojado en Depósito"
            :value="formatNumber(metrics.totalQqMojadoDeposito.value)"
            unit="qq"
            variant="info"
          />
          <MetricCard
            label="Total qq Seco Oreado en Depósito"
            :value="formatNumber(metrics.totalQqOreadoDeposito.value)"
            unit="qq"
            variant="warning"
          />
          <MetricCard
            label="Total qq Seco en Depósito"
            :value="formatNumber(metrics.totalQqSecoDeposito.value)"
            unit="qq"
            variant="primary"
          />
        </div>
      </div>
    </div>
  </UCard>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import type { IngresosMetrics } from '~/composables/useIngresosMetrics'

defineProps<{
  metrics: IngresosMetrics
}>()

type UnitDisplay = 'lb' | 'qq' | 'both'

const unitDisplay = ref<UnitDisplay>('lb')

const unitDisplayLabel = computed(() => {
  switch (unitDisplay.value) {
    case 'lb': return 'lb'
    case 'qq': return 'qq'
    case 'both': return 'lb + qq'
  }
})

function cycleUnitDisplay() {
  if (unitDisplay.value === 'both') {
    unitDisplay.value = 'lb'
  } else if (unitDisplay.value === 'lb') {
    unitDisplay.value = 'qq'
  } else {
    unitDisplay.value = 'both'
  }
}

function formatNumber(value: number): string {
  return value.toLocaleString('en-US', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  })
}

function formatUvaValueNumber(lb: number, qq: number): string {
  switch (unitDisplay.value) {
    case 'lb':
      return Math.round(lb).toLocaleString('en-US')
    case 'qq':
      return formatNumber(qq)
    case 'both':
      return `${Math.round(lb).toLocaleString('en-US')} / ${formatNumber(qq)}`
  }
}

function formatUvaValueUnit(): string {
  switch (unitDisplay.value) {
    case 'lb':
      return 'lb'
    case 'qq':
      return 'qq'
    case 'both':
      return 'lb / qq'
  }
}
</script>