<script setup lang="ts">
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { teamFlagUrl } from '../utils/teamFlag';
import StatusWatermark from './StatusWatermark.vue';
import { matchPhaseLabel, matchPhaseVariant, type MatchPhase } from '../utils/matchPhase';

const props = defineProps<{
  match: {
    id: string;
    homeTeamName: string;
    awayTeamName: string;
    homeTeamCode?: string;
    awayTeamCode?: string;
    homeTeamLogoUrl?: string | null;
    awayTeamLogoUrl?: string | null;
    startTime: string;
    bettingOpen?: boolean;
    matchPhase?: MatchPhase;
    score?: {
      htHome: number;
      htAway: number;
      ftHome: number;
      ftAway: number;
    } | null;
  };
}>();

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()));

const bettingOpen = computed(() => props.match.bettingOpen !== false);

const phase = computed(
  () => props.match.matchPhase ?? (bettingOpen.value ? 'open' : 'closed_pending'),
);

const phaseLabel = computed(() => matchPhaseLabel(t, phase.value));

const liveScoreText = computed(() => {
  const s = props.match.score;
  if (!s) return '';
  return `${s.ftHome} - ${s.ftAway}`;
});
</script>

<template>
  <article
    class="match-card"
    :class="{ 'match-card--phase': phase !== 'open' }"
    @click="emit('bet', match.id)"
  >
    <StatusWatermark
      v-if="phase !== 'open'"
      :label="phaseLabel"
      :variant="matchPhaseVariant(phase)"
      size="sm"
    />

    <div class="teams-row">
      <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>

      <div class="center-col">
        <span class="kickoff">{{ kickoffText }}</span>
        <span v-if="phase !== 'open' && liveScoreText" class="live-score">{{ liveScoreText }}</span>
        <span v-else class="vs">VS</span>
      </div>

      <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"
      :class="bettingOpen ? 'btn-gold-outline' : 'bet-btn--view'"
      @click.stop="emit('bet', match.id)"
    >
      {{ bettingOpen ? t('bet.place_bet_short') : t('bet.view_match') }}
    </button>
  </article>
</template>

<style scoped>
.match-card {
  position: relative;
  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;
  overflow: hidden;
  cursor: pointer;
}

.match-card--phase {
  border-color: rgba(140, 140, 140, 0.35);
}

.teams-row {
  position: relative;
  z-index: 2;
  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;
}

.match-card--phase .team-name {
  color: #d8d8d8;
}

.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);
}

.live-score {
  font-size: 20px;
  font-weight: 900;
  color: #fff;
  line-height: 1;
  letter-spacing: 0.04em;
}

.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 {
  position: relative;
  z-index: 2;
  width: auto;
  min-width: 80px;
  padding: 5px 24px;
  border-radius: 6px;
  font-size: 13px;
  letter-spacing: 0.04em;
  line-height: 1.2;
}

.bet-btn--view {
  background: #1a1a1a;
  border: 1px solid #444;
  color: #aaa;
}
</style>
