<script setup lang="ts">
import { computed, ref, watch, onMounted, onUnmounted } from 'vue';
import { teamFlagUrl } from '../../utils/teamFlag';

const props = defineProps<{
  teamCode: string;
  teamName: string;
  odds: string;
  logoUrl?: string | null;
  disabled?: boolean;
  isWinner?: boolean;
  selectionId?: string;
}>();

const emit = defineEmits<{ pick: [] }>();

const flag = computed(
  () => props.logoUrl?.trim() || teamFlagUrl(props.teamCode, props.teamName),
);
const flagFailed = ref(false);
const imgVisible = ref(false);
const cardRef = ref<HTMLElement | null>(null);
let observer: IntersectionObserver | null = null;

function onFlagError() {
  flagFailed.value = true;
}

watch(
  () => [props.teamCode, props.teamName, props.logoUrl] as const,
  () => {
    flagFailed.value = false;
  },
);

function formatOdds(odds: string) {
  const n = parseFloat(odds);
  return Number.isFinite(n) ? n.toFixed(2) : odds;
}

onMounted(() => {
  if (!cardRef.value) return;
  observer = new IntersectionObserver(
    (entries) => {
      const entry = entries[0];
      if (entry.isIntersecting) {
        imgVisible.value = true;
        observer?.disconnect();
      }
    },
    { rootMargin: '120px' }
  );
  observer.observe(cardRef.value);
});

onUnmounted(() => {
  observer?.disconnect();
});
</script>

<template>
  <button
    ref="cardRef"
    type="button"
    class="option-card"
    :class="{ 'option-card--disabled': disabled, 'option-card--winner': isWinner }"
    :data-bet-selection-id="selectionId || undefined"
    :disabled="disabled"
    @click="emit('pick')"
  >
    <img
      v-if="imgVisible && flag && !flagFailed"
      :src="flag"
      alt=""
      class="flag"
      @error="onFlagError"
    />
    <span v-else class="flag-placeholder" aria-hidden="true">⚽</span>
    <span class="name">{{ teamName }}</span>
    <span class="odds">[ {{ formatOdds(odds) }} ]</span>
  </button>
</template>

<style scoped>
.option-card {
  position: relative;
  width: 100%;
  min-width: 0;
  padding: 10px 4px 8px;
  border-radius: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  text-align: center;
  overflow: hidden;
  transition: all 0.2s ease;
  cursor: pointer;
}

.option-card:hover:not(.option-card--disabled) {
  border-color: var(--border-active);
  background: var(--bg-hover) !important;
}

.option-card:active:not(.option-card--disabled) {
  border-color: var(--primary);
}

.option-card--disabled {
  cursor: default;
  opacity: 0.6;
}

.option-card--winner {
  border-color: var(--primary);
  box-shadow: 0 0 0 1px var(--primary);
}

.flag {
  width: 28px;
  height: 19px;
  object-fit: cover;
  border-radius: 2px;
}

.flag-placeholder {
  width: 28px;
  height: 19px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  opacity: 0.55;
}

.name {
  font-size: 11px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.odds {
  font-size: 12px;
  font-weight: 800;
  color: var(--odds-accent);
}
</style>
