<script setup lang="ts">
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { formatMoney, parseAmount } from '../utils/localeDisplay';
import type { BetHistoryItem } from './BetHistoryCard.vue';

const props = defineProps<{ items: BetHistoryItem[] }>();
const { t, locale } = useI18n();

const stats = computed(() => {
  let total = 0;
  let won = 0;
  let lost = 0;
  let pending = 0;
  let push = 0;
  let totalStake = 0;
  let totalReturn = 0;

  for (const bet of props.items) {
    total++;
    const s = bet.status.toUpperCase();
    if (s === 'WON' || s === 'WIN') won++;
    else if (s === 'LOST' || s === 'LOSE') lost++;
    else if (s === 'PUSH' || s === 'VOID' || s === 'CANCELLED') push++;
    else pending++;
    totalStake += parseAmount(bet.stake);
    if (s === 'WON' || s === 'WIN') totalReturn += parseAmount(bet.actualReturn);
    else if (s === 'PENDING') totalReturn += parseAmount(bet.potentialReturn);
  }

  return { total, won, lost, pending, push, totalStake, totalReturn };
});
</script>

<template>
  <div class="stats-panel">
    <div class="stats-row">
      <div class="stat-item">
        <span class="stat-val">{{ stats.total }}</span>
        <span class="stat-label">{{ t('history.stats_total') }}</span>
      </div>
      <div class="stat-item won">
        <span class="stat-val">{{ stats.won }}</span>
        <span class="stat-label">{{ t('history.stats_won') }}</span>
      </div>
      <div class="stat-item lost">
        <span class="stat-val">{{ stats.lost }}</span>
        <span class="stat-label">{{ t('history.stats_lost') }}</span>
      </div>
      <div class="stat-item pending">
        <span class="stat-val">{{ stats.pending }}</span>
        <span class="stat-label">{{ t('history.stats_pending') }}</span>
      </div>
      <div class="stat-item push">
        <span class="stat-val">{{ stats.push }}</span>
        <span class="stat-label">{{ t('history.stats_push') }}</span>
      </div>
    </div>
    <div class="stats-bar" :class="{ empty: stats.total === 0 }" aria-hidden="true">
      <template v-if="stats.total > 0">
        <div v-if="stats.won > 0" class="bar-seg won" :style="{ flex: stats.won }" />
        <div v-if="stats.lost > 0" class="bar-seg lost" :style="{ flex: stats.lost }" />
        <div v-if="stats.pending > 0" class="bar-seg pending" :style="{ flex: stats.pending }" />
        <div v-if="stats.push > 0" class="bar-seg push" :style="{ flex: stats.push }" />
      </template>
    </div>
    <div class="stats-row secondary">
      <div class="stat-item">
        <span class="stat-val small">{{ formatMoney(stats.totalStake, locale) }}</span>
        <span class="stat-label">{{ t('history.stats_stake') }}</span>
      </div>
      <div class="stat-item">
        <span class="stat-val small gold">{{ formatMoney(stats.totalReturn, locale) }}</span>
        <span class="stat-label">{{ t('history.stats_return') }}</span>
      </div>
    </div>
  </div>
</template>

<style scoped>
.stats-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 12px 10px;
  margin-bottom: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.stats-row {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
}

.stats-row.secondary {
  margin-bottom: 0;
  margin-top: 8px;
  justify-content: space-around;
}

.stat-item {
  flex: 1;
  text-align: center;
  min-width: 0;
}

.stat-val {
  display: block;
  font-size: 18px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.stat-val.small {
  font-size: 14px;
  color: var(--text-muted);
}

.stat-val.gold {
  color: var(--primary);
}

.stat-item.won .stat-val { color: var(--success); }
.stat-item.lost .stat-val { color: var(--danger); }
.stat-item.pending .stat-val { color: var(--warning); }
.stat-item.push .stat-val { color: var(--text-muted); }

.stat-label {
  display: block;
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.04em;
  margin-top: 2px;
}

.stats-bar {
  display: flex;
  height: 4px;
  border-radius: 2px;
  overflow: hidden;
  gap: 2px;
}

.stats-bar.empty {
  background: var(--bg-elevated);
}

.bar-seg {
  border-radius: 2px;
  transition: flex 0.3s ease;
}

.bar-seg.won { background: var(--success); }
.bar-seg.lost { background: var(--danger); }
.bar-seg.pending { background: var(--warning); }
.bar-seg.push { background: var(--text-muted); }
</style>
