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

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 | null;
      htAway: number | null;
      ftHome: number | null;
      ftAway: number | null;
      homeCorners?: number | null;
      awayCorners?: number | null;
      homeYellowCards?: number | null;
      awayYellowCards?: number | null;
      homeRedCards?: number | null;
      awayRedCards?: number | null;
      homeCards?: number | null;
      awayCards?: number | null;
    } | null;
  };
}>();

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

const { t, locale } = useI18n();

function formatKickoff(startTime: string) {
  return formatLocalMatchDateTime(startTime, locale.value, {
    variant: 'compact',
    todayLabel: t('bet.today'),
  });
}

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 || s.ftHome == null || s.ftAway == null) return '';
  return `${s.ftHome} - ${s.ftAway}`;
});
</script>

<template>
  <article
    class="match-card"
    :class="{ 'match-card--phase': phase !== 'open' }"
    @click="emit('bet', match.id)"
  >
    <span v-if="phase === 'open'" class="status-tag status-tag--open">{{ t('bet.status_open') }}</span>
    <span v-else-if="phase === 'settled'" class="status-tag status-tag--settled">{{ phaseLabel }}</span>
    <span v-else class="status-tag status-tag--pending">{{ phaseLabel }}</span>

    <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=""
          loading="lazy"
          decoding="async"
        />
      </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=""
          loading="lazy"
          decoding="async"
        />
      </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: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 6px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 0;
  overflow: hidden;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s, box-shadow 0.15s;
}

.match-card::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(244, 162, 97, 0.02) 0%, transparent 50%, rgba(59, 130, 246, 0.02) 100%);
  pointer-events: none;
  z-index: 0;
}

.match-card--phase {
  opacity: 0.95;
}

.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: 4px;
  min-width: 0;
  transform: translateY(6px);
}

.team-name {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
  word-break: break-word;
  text-align: center;
  padding: 0 4px;
  align-self: stretch;
}

.match-card--phase .team-name {
  color: var(--text-dim);
}

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

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

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

.kickoff {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.15;
  white-space: normal;
  text-align: center;
  max-width: 80px;
  overflow-wrap: anywhere;
}

.live-score {
  font-size: 18px;
  font-weight: 900;
  color: var(--primary);
  line-height: 1;
}

.vs {
  font-size: 18px;
  font-weight: 900;
  color: var(--primary);
  line-height: 1;
}

.status-tag {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 3;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 0 6px 0 8px;
  line-height: 1.3;
  white-space: nowrap;
}

.status-tag--open {
  background: rgba(255, 179, 0, 0.15);
  color: #FFB300;
  border-bottom: 1px solid rgba(255, 179, 0, 0.3);
  border-left: 1px solid rgba(255, 179, 0, 0.3);
}

.status-tag--settled {
  background: var(--bg-elevated);
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
  border-left: 1px solid var(--border);
}

.status-tag--pending {
  background: rgba(255, 179, 0, 0.15);
  color: #FFB300;
  border-bottom: 1px solid rgba(255, 179, 0, 0.3);
  border-left: 1px solid rgba(255, 179, 0, 0.3);
}

.bet-btn {
  position: relative;
  z-index: 2;
  width: auto;
  min-width: 72px;
  padding: 4px 20px;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.2;
}

.bet-btn--view {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
</style>
