<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: linear-gradient(180deg, #1a1a1a 0%, #0d0d0d 100%);
  border: 1px solid rgba(140, 140, 140, 0.35);
  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::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(100, 100, 100, 0.08) 0%, transparent 50%, rgba(80, 80, 80, 0.05) 100%);
  pointer-events: none;
  z-index: 0;
}

.match-card--phase {
  /* inherits base dark style */
}

.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: #fff;
  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: #fff;
  line-height: 1.15;
  white-space: normal;
  text-align: center;
  max-width: 96px;
  overflow-wrap: anywhere;
  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);
}

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

.status-tag--open {
  background: linear-gradient(135deg, #e8c84a, #d4a017);
  color: #1a1a1a;
  box-shadow: 0 2px 6px rgba(212, 160, 23, 0.4);
}

.status-tag--settled {
  background: linear-gradient(180deg, #2a2a3a 0%, #1a1a28 100%);
  color: #8a9ab8;
  border-bottom: 1px solid rgba(120, 140, 180, 0.3);
  border-left: 1px solid rgba(120, 140, 180, 0.3);
}

.status-tag--pending {
  background: linear-gradient(180deg, #3a3a2a 0%, #28251a 100%);
  color: #c8a84e;
  border-bottom: 1px solid rgba(200, 168, 78, 0.3);
  border-left: 1px solid rgba(200, 168, 78, 0.3);
}

.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>
