<script setup lang="ts">
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { teamFlagUrl } from '../utils/teamFlag';

const props = defineProps<{
  match: {
    id: string;
    homeTeamName: string;
    awayTeamName: string;
    homeTeamCode?: string;
    awayTeamCode?: string;
    homeTeamLogoUrl?: string | null;
    awayTeamLogoUrl?: string | null;
    startTime: string;
  };
}>();

const emit = defineEmits<{ bet: [id: string] }>();

const { t, locale } = useI18n();

function formatKickoff(startTime: string) {
  const d = new Date(startTime);
  const now = new Date();
  const isToday =
    d.getFullYear() === now.getFullYear() &&
    d.getMonth() === now.getMonth() &&
    d.getDate() === now.getDate();
  const timeStr = d.toLocaleTimeString(locale.value, { hour: '2-digit', minute: '2-digit' });
  if (isToday) return `${t('bet.today')} ${timeStr}`;
  const dateStr = d.toLocaleDateString(locale.value, { month: 'numeric', day: 'numeric' });
  return `${dateStr} ${timeStr}`;
}

const kickoffText = computed(() => formatKickoff(props.match.startTime));

const homeFlagUrl = computed(() =>
  teamFlagUrl(props.match.homeTeamCode, props.match.homeTeamName, props.match.homeTeamLogoUrl),
);
const awayFlagUrl = computed(() =>
  teamFlagUrl(props.match.awayTeamCode, props.match.awayTeamName, props.match.awayTeamLogoUrl),
);

const homeIsLogo = computed(() => Boolean(props.match.homeTeamLogoUrl?.trim()));
const awayIsLogo = computed(() => Boolean(props.match.awayTeamLogoUrl?.trim()));
</script>

<template>
  <article class="match-card">
    <div class="teams-row">
      <!-- Home -->
      <div class="team">
        <span class="team-name">{{ match.homeTeamName }}</span>
        <img
          v-if="homeFlagUrl"
          :src="homeFlagUrl"
          class="team-flag"
          :class="{ 'flag-logo': homeIsLogo }"
          alt=""
        />
      </div>

      <!-- Center -->
      <div class="center-col">
        <span class="kickoff">{{ kickoffText }}</span>
        <span class="vs">VS</span>
      </div>

      <!-- Away -->
      <div class="team">
        <span class="team-name">{{ match.awayTeamName }}</span>
        <img
          v-if="awayFlagUrl"
          :src="awayFlagUrl"
          class="team-flag"
          :class="{ 'flag-logo': awayIsLogo }"
          alt=""
        />
      </div>
    </div>

    <button type="button" class="bet-btn btn-gold-outline" @click="emit('bet', match.id)">
      {{ t('bet.place_bet_short') }}
    </button>
  </article>
</template>

<style scoped>
.match-card {
  background: #0d0d0d;
  border: 1px solid rgba(255, 215, 0, 0.25);
  border-radius: 6px;
  padding: 10px 8px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.teams-row {
  display: flex;
  align-items: center;
  width: 100%;
  gap: 4px;
}

.team {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 0;
  transform: translateY(8px);
}

.team-name {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 900;
  color: var(--primary-light);
  line-height: 1.2;
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.95);
  word-break: break-word;
  text-align: center;
  padding: 0 6px;
  align-self: stretch;
}

.team-flag {
  width: 72px;
  height: 48px;
  object-fit: cover;
  border-radius: 3px;
  display: block;
}

.team-flag.flag-logo {
  width: 48px;
  height: 48px;
  object-fit: contain;
}

.center-col {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 56px;
}

.kickoff {
  font-size: 13px;
  font-weight: 700;
  color: #b0a060;
  line-height: 1;
  white-space: nowrap;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
}

.vs {
  font-size: 22px;
  font-weight: 900;
  color: #fff;
  letter-spacing: 0.08em;
  line-height: 1;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.9);
}

.bet-btn {
  width: auto;
  min-width: 80px;
  padding: 5px 24px;
  border-radius: 6px;
  font-size: 13px;
  letter-spacing: 0.04em;
  line-height: 1.2;
}
</style>
