All files / src/components MatchPreview.vue

82.65% Statements 324/392
26.66% Branches 4/15
20% Functions 1/5
82.65% Lines 324/392

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 4691x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 48x 48x 48x         48x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x                     1x   1x   1x 1x 1x 1x 1x   1x   1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x   1x   1x                             1x   1x   24x 24x 24x 24x 24x 24x 24x 24x   24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x   24x 24x 24x 24x   1x 1x 1x 1x 1x 1x   1x    
<template>
  <div class="match-preview">
    <!-- Loading -->
    <div
      v-if="loading"
      class="flex flex-col items-center justify-center py-12"
      data-testid="preview-loading"
    >
      <div
        class="w-8 h-8 border-2 border-gray-300 border-t-blue-500 rounded-full animate-spin mb-3"
      ></div>
      <p class="text-gray-500 text-sm">Loading match preview...</p>
    </div>
 
    <!-- Error -->
    <div
      v-else-if="error"
      class="flex flex-col items-center justify-center py-12 text-center"
      data-testid="preview-error"
    >
      <div
        class="w-10 h-10 rounded-full bg-red-100 flex items-center justify-center text-red-500 font-bold mb-3"
      >
        !
      </div>
      <p class="text-gray-500 text-sm mb-3">{{ error }}</p>
      <button
        @click="fetchPreview"
        class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg text-sm font-medium transition-colors"
      >
        Retry
      </button>
    </div>
 
    <!-- Content -->
    <div v-else-if="preview" data-testid="preview-content">
      <!-- Tab bar -->
      <div class="border-b border-gray-200 mb-4">
        <nav class="flex space-x-1" aria-label="Match preview sections">
          <button
            v-for="tab in tabs"
            :key="tab.id"
            @click="activeTab = tab.id"
            :data-testid="`tab-${tab.id}`"
            :class="[
              'py-2 px-3 text-sm font-medium border-b-2 transition-colors whitespace-nowrap',
              activeTab === tab.id
                ? 'border-blue-600 text-blue-600'
                : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300',
            ]"
          >
            {{ tab.label }}
            <span
              v-if="tab.count !== null"
              class="ml-1 text-xs px-1.5 py-0.5 rounded-full"
              :class="
                activeTab === tab.id
                  ? 'bg-blue-100 text-blue-700'
                  : 'bg-gray-100 text-gray-500'
              "
              >{{ tab.count }}</span
            >
          </button>
        </nav>
      </div>
 
      <!-- ── Recent Form ── -->
      <div v-if="activeTab === 'form'" data-testid="tab-panel-form">
        <div class="grid grid-cols-2 gap-3">
          <!-- Home team column -->
          <div>
            <h3
              class="text-xs font-semibold text-gray-500 uppercase tracking-wide mb-2 truncate"
            >
              {{ homeTeamName }}
            </h3>
            <div
              v-if="preview.home_team_recent.length === 0"
              class="text-xs text-gray-400 italic"
            >
              No recent matches
            </div>
            <div
              v-for="match in preview.home_team_recent"
              :key="match.id"
              class="mb-2"
              data-testid="home-recent-match"
            >
              <MatchResultCard :match="match" :perspectiveTeamId="homeTeamId" />
            </div>
          </div>
 
          <!-- Away team column -->
          <div>
            <h3
              class="text-xs font-semibold text-gray-500 uppercase tracking-wide mb-2 truncate"
            >
              {{ awayTeamName }}
            </h3>
            <div
              v-if="preview.away_team_recent.length === 0"
              class="text-xs text-gray-400 italic"
            >
              No recent matches
            </div>
            <div
              v-for="match in preview.away_team_recent"
              :key="match.id"
              class="mb-2"
              data-testid="away-recent-match"
            >
              <MatchResultCard :match="match" :perspectiveTeamId="awayTeamId" />
            </div>
          </div>
        </div>
      </div>
 
      <!-- ── Common Opponents ── -->
      <div v-else-if="activeTab === 'common'" data-testid="tab-panel-common">
        <div
          v-if="preview.common_opponents.length === 0"
          class="text-sm text-gray-500 text-center py-8"
        >
          No common opponents found for this season.
        </div>
        <div
          v-for="opp in preview.common_opponents"
          :key="opp.opponent_id"
          class="mb-5"
          data-testid="common-opponent-section"
        >
          <!-- Opponent header -->
          <div
            class="flex items-center gap-2 mb-2 pb-1 border-b border-gray-100"
          >
            <span class="inline-block w-2 h-2 rounded-full bg-gray-400"></span>
            <span class="text-sm font-semibold text-gray-800"
              >vs {{ opp.opponent_name }}</span
            >
          </div>
 
          <div class="grid grid-cols-2 gap-3">
            <!-- Home team vs opponent -->
            <div>
              <p class="text-xs text-gray-400 mb-1 truncate">
                {{ homeTeamName }}
              </p>
              <div
                v-if="opp.home_team_matches.length === 0"
                class="text-xs text-gray-400 italic"
              >
                No matches
              </div>
              <div
                v-for="match in opp.home_team_matches"
                :key="match.id"
                class="mb-1.5"
              >
                <MatchResultCard
                  :match="match"
                  :perspectiveTeamId="homeTeamId"
                  compact
                />
              </div>
            </div>
            <!-- Away team vs opponent -->
            <div>
              <p class="text-xs text-gray-400 mb-1 truncate">
                {{ awayTeamName }}
              </p>
              <div
                v-if="opp.away_team_matches.length === 0"
                class="text-xs text-gray-400 italic"
              >
                No matches
              </div>
              <div
                v-for="match in opp.away_team_matches"
                :key="match.id"
                class="mb-1.5"
              >
                <MatchResultCard
                  :match="match"
                  :perspectiveTeamId="awayTeamId"
                  compact
                />
              </div>
            </div>
          </div>
        </div>
      </div>
 
      <!-- ── Head-to-Head ── -->
      <div v-else-if="activeTab === 'h2h'" data-testid="tab-panel-h2h">
        <div
          v-if="preview.head_to_head.length === 0"
          class="text-sm text-gray-500 text-center py-8"
        >
          No previous meetings found.
        </div>
 
        <!-- Summary row -->
        <div
          v-if="preview.head_to_head.length > 0"
          class="flex justify-around text-center mb-4 p-3 bg-gray-50 rounded-lg"
        >
          <div>
            <span class="block text-xl font-bold text-blue-600">{{
              h2hStats.homeWins
            }}</span>
            <span class="text-xs text-gray-500 truncate block max-w-[80px]">{{
              homeTeamName
            }}</span>
          </div>
          <div>
            <span class="block text-xl font-bold text-gray-500">{{
              h2hStats.draws
            }}</span>
            <span class="text-xs text-gray-500">Draws</span>
          </div>
          <div>
            <span class="block text-xl font-bold text-red-500">{{
              h2hStats.awayWins
            }}</span>
            <span class="text-xs text-gray-500 truncate block max-w-[80px]">{{
              awayTeamName
            }}</span>
          </div>
        </div>
 
        <!-- Match list -->
        <div
          v-for="match in preview.head_to_head"
          :key="match.id"
          class="mb-2"
          data-testid="h2h-match"
        >
          <H2HMatchRow
            :match="match"
            :homeTeamId="homeTeamId"
            :awayTeamId="awayTeamId"
          />
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup>
import { ref, computed, watch, onMounted } from 'vue';
import { useAuthStore } from '@/stores/auth';
import { getApiBaseUrl } from '@/config/api';
 
// ─── Sub-components defined inline ────────────────────────────────────────────
 
/**
 * MatchResultCard - compact match result from one team's perspective.
 * Shows: W/L/D chip | score | opponent name | date
 */
const MatchResultCard = {
  props: {
    match: { type: Object, required: true },
    perspectiveTeamId: { type: Number, required: true },
    compact: { type: Boolean, default: false },
  },
  setup(props) {
    const outcome = computed(() => {
      const m = props.match;
      if (m.home_score === null || m.away_score === null) return null;
      const isHome = m.home_team_id === props.perspectiveTeamId;
      const teamScore = isHome ? m.home_score : m.away_score;
      const oppScore = isHome ? m.away_score : m.home_score;
      if (teamScore > oppScore) return 'W';
      if (teamScore < oppScore) return 'L';
      return 'D';
    });
 
    const opponent = computed(() => {
      const m = props.match;
      return m.home_team_id === props.perspectiveTeamId
        ? m.away_team_name
        : m.home_team_name;
    });
 
    const scoreDisplay = computed(() => {
      const m = props.match;
      if (m.home_score === null || m.away_score === null) return '- : -';
      return `${m.home_score} - ${m.away_score}`;
    });
 
    const outcomeClass = computed(() => {
      if (outcome.value === 'W') return 'bg-green-100 text-green-700';
      if (outcome.value === 'L') return 'bg-red-100 text-red-700';
      if (outcome.value === 'D') return 'bg-gray-100 text-gray-600';
      return 'bg-gray-100 text-gray-400';
    });
 
    const formattedDate = computed(() => {
      if (!props.match.match_date) return '';
      const d = new Date(props.match.match_date + 'T00:00:00');
      return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
    });
 
    return { outcome, opponent, scoreDisplay, outcomeClass, formattedDate };
  },
  template: `
    <div class="flex items-center gap-1.5 p-1.5 rounded bg-white border border-gray-100 shadow-sm text-xs">
      <span :class="['font-bold w-4 text-center shrink-0', outcomeClass]" style="border-radius:2px">
        {{ outcome ?? '?' }}
      </span>
      <span class="font-mono font-semibold text-gray-700 shrink-0">{{ scoreDisplay }}</span>
      <span class="text-gray-600 truncate flex-1">{{ opponent }}</span>
      <span class="text-gray-400 shrink-0">{{ formattedDate }}</span>
    </div>
  `,
};
 
/**
 * H2HMatchRow - single head-to-head match row.
 * Shows home and away team with score centred, plus season and date.
 */
const H2HMatchRow = {
  props: {
    match: { type: Object, required: true },
    homeTeamId: { type: Number, required: true },
    awayTeamId: { type: Number, required: true },
  },
  setup(props) {
    const formattedDate = computed(() => {
      if (!props.match.match_date) return '';
      const d = new Date(props.match.match_date + 'T00:00:00');
      return d.toLocaleDateString('en-US', {
        month: 'short',
        day: 'numeric',
        year: 'numeric',
      });
    });
 
    const winnerClass = teamId => {
      const m = props.match;
      if (m.home_score === null || m.away_score === null) return '';
      const isHome = m.home_team_id === teamId;
      const score = isHome ? m.home_score : m.away_score;
      const opp = isHome ? m.away_score : m.home_score;
      if (score > opp) return 'font-bold text-gray-900';
      if (score < opp) return 'text-gray-400';
      return 'text-gray-700';
    };
 
    return { formattedDate, winnerClass };
  },
  template: `
    <div class="flex items-center gap-2 px-3 py-2 rounded-lg bg-white border border-gray-100 shadow-sm text-xs">
      <span :class="['flex-1 text-right truncate', winnerClass(match.home_team_id)]">{{ match.home_team_name }}</span>
      <span class="font-mono font-semibold text-gray-800 shrink-0 bg-gray-50 px-2 py-0.5 rounded">
        {{ match.home_score ?? '-' }} – {{ match.away_score ?? '-' }}
      </span>
      <span :class="['flex-1 truncate', winnerClass(match.away_team_id)]">{{ match.away_team_name }}</span>
      <div class="text-right shrink-0 ml-2">
        <div class="text-gray-400">{{ formattedDate }}</div>
        <div class="text-gray-300">{{ match.season_name }}</div>
      </div>
    </div>
  `,
};
 
// ─── Props ────────────────────────────────────────────────────────────────────
 
const props = defineProps({
  homeTeamId: { type: Number, required: true },
  homeTeamName: { type: String, default: 'Home Team' },
  awayTeamId: { type: Number, required: true },
  awayTeamName: { type: String, default: 'Away Team' },
  /** Season ID scopes "recent form" and "common opponents". Null = all seasons. */
  seasonId: { type: Number, default: null },
  /** Optional: narrow to a specific age group */
  ageGroupId: { type: Number, default: null },
  /** How many recent matches to fetch per team */
  recentCount: { type: Number, default: 5 },
});
 
// ─── State ────────────────────────────────────────────────────────────────────
 
const authStore = useAuthStore();
const preview = ref(null);
const loading = ref(false);
const error = ref(null);
const activeTab = ref('form');
 
// ─── Tabs ────────────────────────────────────────────────────────────────────
 
const tabs = computed(() => [
  { id: 'form', label: 'Recent Form', count: null },
  {
    id: 'common',
    label: 'Common Opponents',
    count: preview.value ? (preview.value.common_opponents?.length ?? 0) : null,
  },
  {
    id: 'h2h',
    label: 'Head-to-Head',
    count: preview.value ? (preview.value.head_to_head?.length ?? 0) : null,
  },
]);
 
// ─── Head-to-head summary stats ───────────────────────────────────────────────
 
const h2hStats = computed(() => {
  if (!preview.value) return { homeWins: 0, awayWins: 0, draws: 0 };
  return preview.value.head_to_head.reduce(
    (acc, m) => {
      if (m.home_score === null || m.away_score === null) return acc;
      const homeIsOurHome = m.home_team_id === props.homeTeamId;
      const homeScore = homeIsOurHome ? m.home_score : m.away_score;
      const awayScore = homeIsOurHome ? m.away_score : m.home_score;
      if (homeScore > awayScore) acc.homeWins++;
      else if (homeScore < awayScore) acc.awayWins++;
      else acc.draws++;
      return acc;
    },
    { homeWins: 0, awayWins: 0, draws: 0 }
  );
});
 
// ─── Data fetching ───────────────────────────────────────────────────────────
 
async function fetchPreview() {
  loading.value = true;
  error.value = null;
  try {
    const params = new URLSearchParams();
    if (props.seasonId) params.set('season_id', props.seasonId);
    if (props.ageGroupId) params.set('age_group_id', props.ageGroupId);
    params.set('recent_count', props.recentCount);
 
    const url = `${getApiBaseUrl()}/api/matches/preview/${props.homeTeamId}/${props.awayTeamId}?${params}`;
    const data = await authStore.apiRequest(url);
    // Normalize to guarantee expected arrays are always present
    preview.value = {
      home_team_id: data?.home_team_id ?? props.homeTeamId,
      away_team_id: data?.away_team_id ?? props.awayTeamId,
      home_team_recent: Array.isArray(data?.home_team_recent)
        ? data.home_team_recent
        : [],
      away_team_recent: Array.isArray(data?.away_team_recent)
        ? data.away_team_recent
        : [],
      common_opponents: Array.isArray(data?.common_opponents)
        ? data.common_opponents
        : [],
      head_to_head: Array.isArray(data?.head_to_head) ? data.head_to_head : [],
    };
  } catch (err) {
    error.value = err.message || 'Failed to load match preview';
  } finally {
    loading.value = false;
  }
}
 
// Refetch when key props change
watch(
  () => [props.homeTeamId, props.awayTeamId, props.seasonId, props.ageGroupId],
  () => fetchPreview(),
  { immediate: false }
);
 
onMounted(fetchPreview);
</script>