<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useI18n } from 'vue-i18n';
import type { OutrightEvent, OutrightSelection } from '../outright/OutrightEventSection.vue';
import saishiImg from '../../assets/images/saishi.webp';

const props = defineProps<{
  event: OutrightEvent;
}>();

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

const { t } = useI18n();

const headTitle = computed(() => {
  const raw = props.event.title.replace(/^\*+/, '').trim();
  return raw || props.event.leagueName || t('bet.tab_outright');
});

const teamCount = computed(() => props.event.selectionCount ?? props.event.selections.length);

const isSettled = computed(
  () => props.event.bettingOpen === false || props.event.status === 'SETTLED',
);

const MIN_BTN_W = 52;
const GAP = 4;

const cardRef = ref<HTMLElement | null>(null);
const cardWidth = ref(400);
let ro: ResizeObserver | null = null;

onMounted(() => {
  if (!cardRef.value) return;
  ro = new ResizeObserver(([entry]) => {
    cardWidth.value = entry.contentRect.width;
  });
  ro.observe(cardRef.value);
});

onUnmounted(() => ro?.disconnect());

const maxVisible = computed(() => {
  const panelW = cardWidth.value - 24;
  return Math.max(1, Math.min(5, Math.floor((panelW + GAP) / (MIN_BTN_W + GAP))));
});

const topSelections = computed(() => {
  if (!props.event.selections) return [];
  return [...props.event.selections]
    .sort((a, b) => {
      const oa = parseFloat(a.odds) || 9999;
      const ob = parseFloat(b.odds) || 9999;
      return oa - ob;
    })
    .slice(0, maxVisible.value);
});
</script>

<template>
  <article ref="cardRef" class="outright-event-card" :class="{ settled: isSettled }">
    <div class="card-clickable-area" @click="emit('open')">
      <div class="card-main">
        <div class="title-row">
          <span class="title">{{ headTitle }}</span>
          <span v-if="isSettled" class="settled-tag">{{ t('bet.outright_settled') }}</span>
        </div>
        <p v-if="event.leagueName && event.leagueName !== headTitle" class="league">{{ event.leagueName }}</p>
        <p class="meta">{{ t('bet.outright_teams_count', { n: teamCount }) }}</p>
      </div>
      <img :src="saishiImg" alt="" class="saishi" />
    </div>

    <div v-if="!isSettled && topSelections.length" class="quick-bet-panel">
      <div class="quick-bet-title">
        {{ t('bet.quick_bet') }}
        <span class="quick-bet-hint">（{{ t('bet.popular_teams') }}）</span>
      </div>
      <div class="odds-row">
        <button
          v-for="sel in topSelections"
          :key="sel.id"
          type="button"
          class="odds-btn"
          @click.stop="emit('pick', sel)"
        >
          <span class="selection-name" :title="sel.teamName">{{ sel.teamName }}</span>
          <span class="selection-odds">{{ sel.odds }}</span>
        </button>
      </div>
      <button type="button" class="view-all-btn" @click.stop="emit('open')">
        {{ t('bet.outright_view_all') }} →
      </button>
    </div>
  </article>
</template>

<style scoped>
.outright-event-card {
  position: relative;
  width: 100%;
  min-height: 140px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-card);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

@media (hover: hover) {
  .outright-event-card:hover {
    border-color: var(--border-active);
    box-shadow: 0 4px 12px rgba(0, 61, 107, 0.1);
  }

  .outright-event-card:hover .card-clickable-area {
    transform: translateY(-10px);
  }

  .outright-event-card:hover .league,
  .outright-event-card:hover .meta {
    opacity: 0;
  }

  .outright-event-card:hover .saishi {
    transform: scale(0.9);
    opacity: 0;
  }

  .outright-event-card:hover .quick-bet-panel {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
  }
}

.card-clickable-area {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  cursor: pointer;
  flex: 1;
  transition: transform 0.25s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.card-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.title {
  font-size: 13px;
  font-weight: 800;
  color: var(--primary-light);
  line-height: 1.35;
  transition: color 0.2s;
}

.outright-event-card:hover .title {
  color: var(--primary);
}

.settled-tag {
  font-size: 10px;
  font-weight: 800;
  color: var(--odds-accent);
  border: 1px solid rgba(201, 162, 39, 0.45);
  border-radius: 999px;
  padding: 1px 7px;
}

.league,
.meta {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  transition: opacity 0.15s ease;
}

.league {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.saishi {
  flex-shrink: 0;
  height: 44px;
  width: auto;
  max-width: 40px;
  object-fit: contain;
  opacity: 0.85;
  transition: transform 0.25s ease, opacity 0.2s ease;
}

.quick-bet-panel {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 10px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transform: translateY(12px);
  transition: opacity 0.2s ease, transform 0.22s cubic-bezier(0.25, 0.8, 0.25, 1);
  z-index: 5;
}

.quick-bet-title {
  font-size: 9px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.quick-bet-hint {
  font-size: 9px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: none;
  letter-spacing: 0;
  margin-left: 1px;
}

.odds-row {
  display: flex;
  gap: 4px;
  width: 100%;
  overflow: hidden;
}

.odds-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  gap: 1px;
  padding: 2px 4px;
  border-radius: 4px;
  background: var(--bg-body);
  border: 1px solid var(--border);
  cursor: pointer;
  min-height: 32px;
  transition: all 0.2s ease;
}

.odds-btn:hover {
  background: rgba(0, 102, 204, 0.08);
  border-color: var(--border-active);
}

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

.odds-btn:hover .selection-name {
  color: var(--text);
}

.selection-odds {
  font-size: 11px;
  font-weight: 900;
  color: var(--primary);
  line-height: 1.1;
}

.view-all-btn {
  align-self: flex-end;
  font-size: 10px;
  font-weight: 700;
  color: var(--primary-light);
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px 0;
  opacity: 0.85;
  transition: opacity 0.15s ease;
}

.view-all-btn:hover {
  opacity: 1;
}
</style>
