<template>
  <div class="current-situation">
    <!-- Mobile Layout (Stacked) -->
    <div class="lg:hidden space-y-3">
      <!-- Current Pitcher Card -->
      <div
        v-if="currentPitcher"
        class="bg-gradient-to-br from-blue-50 to-blue-100 dark:from-blue-900/20 dark:to-blue-800/20 rounded-xl p-4 border-2 border-blue-200 dark:border-blue-700 shadow-md"
      >
        <div class="flex items-center gap-3">
          <!-- Pitcher Badge -->
          <div class="w-12 h-12 bg-blue-500 rounded-full flex items-center justify-center text-white font-bold text-lg shadow-lg flex-shrink-0">
            P
          </div>

          <!-- Pitcher Info -->
          <div class="flex-1 min-w-0">
            <div class="text-xs font-semibold text-blue-600 dark:text-blue-400 uppercase tracking-wide mb-0.5">
              Pitching
            </div>
            <div class="text-base font-bold text-gray-900 dark:text-white truncate">
              {{ currentPitcher.player.name }}
            </div>
            <div class="text-xs text-gray-600 dark:text-gray-400">
              {{ currentPitcher.position }}
              <span v-if="currentPitcher.player.team" class="ml-1">• {{ currentPitcher.player.team }}</span>
            </div>
          </div>

          <!-- Player Image (if available) -->
          <div
            v-if="currentPitcher.player.image"
            class="w-14 h-14 rounded-lg overflow-hidden border-2 border-white dark:border-gray-700 shadow-md flex-shrink-0"
          >
            <img
              :src="currentPitcher.player.image"
              :alt="currentPitcher.player.name"
              class="w-full h-full object-cover"
              @error="handleImageError"
            />
          </div>
        </div>
      </div>

      <!-- VS Indicator -->
      <div class="flex items-center justify-center">
        <div class="px-4 py-1 bg-gray-800 dark:bg-gray-700 text-white rounded-full text-xs font-bold shadow-lg">
          VS
        </div>
      </div>

      <!-- Current Batter Card -->
      <div
        v-if="currentBatter"
        class="bg-gradient-to-br from-red-50 to-red-100 dark:from-red-900/20 dark:to-red-800/20 rounded-xl p-4 border-2 border-red-200 dark:border-red-700 shadow-md"
      >
        <div class="flex items-center gap-3">
          <!-- Batter Badge -->
          <div class="w-12 h-12 bg-red-500 rounded-full flex items-center justify-center text-white font-bold text-lg shadow-lg flex-shrink-0">
            B
          </div>

          <!-- Batter Info -->
          <div class="flex-1 min-w-0">
            <div class="text-xs font-semibold text-red-600 dark:text-red-400 uppercase tracking-wide mb-0.5">
              At Bat
            </div>
            <div class="text-base font-bold text-gray-900 dark:text-white truncate">
              {{ currentBatter.player.name }}
            </div>
            <div class="text-xs text-gray-600 dark:text-gray-400">
              {{ currentBatter.position }}
              <span v-if="currentBatter.batting_order" class="ml-1">• Batting {{ currentBatter.batting_order }}</span>
            </div>
          </div>

          <!-- Player Image (if available) -->
          <div
            v-if="currentBatter.player.image"
            class="w-14 h-14 rounded-lg overflow-hidden border-2 border-white dark:border-gray-700 shadow-md flex-shrink-0"
          >
            <img
              :src="currentBatter.player.image"
              :alt="currentBatter.player.name"
              class="w-full h-full object-cover"
              @error="handleImageError"
            />
          </div>
        </div>
      </div>
    </div>

    <!-- Desktop Layout (Side-by-Side) -->
    <div class="hidden lg:grid lg:grid-cols-2 gap-6">
      <!-- Current Pitcher Card -->
      <div
        v-if="currentPitcher"
        class="bg-gradient-to-br from-blue-50 to-blue-100 dark:from-blue-900/20 dark:to-blue-800/20 rounded-xl p-6 border-2 border-blue-200 dark:border-blue-700 shadow-lg"
      >
        <div class="flex items-start gap-4">
          <!-- Pitcher Badge -->
          <div class="w-16 h-16 bg-blue-500 rounded-full flex items-center justify-center text-white font-bold text-2xl shadow-xl flex-shrink-0">
            P
          </div>

          <!-- Pitcher Details -->
          <div class="flex-1 min-w-0">
            <div class="text-sm font-semibold text-blue-600 dark:text-blue-400 uppercase tracking-wide mb-1">
              Pitching
            </div>
            <div class="text-xl font-bold text-gray-900 dark:text-white mb-1 truncate">
              {{ currentPitcher.player.name }}
            </div>
            <div class="text-sm text-gray-600 dark:text-gray-400">
              {{ currentPitcher.position }}
              <span v-if="currentPitcher.player.team" class="ml-2">• {{ currentPitcher.player.team }}</span>
            </div>
            <div v-if="currentPitcher.player.manager" class="text-xs text-gray-500 dark:text-gray-500 mt-1">
              Manager: {{ currentPitcher.player.manager }}
            </div>
          </div>

          <!-- Player Image -->
          <div
            v-if="currentPitcher.player.image"
            class="w-20 h-20 rounded-xl overflow-hidden border-2 border-white dark:border-gray-700 shadow-xl flex-shrink-0"
          >
            <img
              :src="currentPitcher.player.image"
              :alt="currentPitcher.player.name"
              class="w-full h-full object-cover"
              @error="handleImageError"
            />
          </div>
        </div>
      </div>

      <!-- Current Batter Card -->
      <div
        v-if="currentBatter"
        class="bg-gradient-to-br from-red-50 to-red-100 dark:from-red-900/20 dark:to-red-800/20 rounded-xl p-6 border-2 border-red-200 dark:border-red-700 shadow-lg"
      >
        <div class="flex items-start gap-4">
          <!-- Batter Badge -->
          <div class="w-16 h-16 bg-red-500 rounded-full flex items-center justify-center text-white font-bold text-2xl shadow-xl flex-shrink-0">
            B
          </div>

          <!-- Batter Details -->
          <div class="flex-1 min-w-0">
            <div class="text-sm font-semibold text-red-600 dark:text-red-400 uppercase tracking-wide mb-1">
              At Bat
            </div>
            <div class="text-xl font-bold text-gray-900 dark:text-white mb-1 truncate">
              {{ currentBatter.player.name }}
            </div>
            <div class="text-sm text-gray-600 dark:text-gray-400">
              {{ currentBatter.position }}
              <span v-if="currentBatter.batting_order" class="ml-2">• Batting {{ currentBatter.batting_order }}</span>
            </div>
            <div v-if="currentBatter.player.manager" class="text-xs text-gray-500 dark:text-gray-500 mt-1">
              Manager: {{ currentBatter.player.manager }}
            </div>
          </div>

          <!-- Player Image -->
          <div
            v-if="currentBatter.player.image"
            class="w-20 h-20 rounded-xl overflow-hidden border-2 border-white dark:border-gray-700 shadow-xl flex-shrink-0"
          >
            <img
              :src="currentBatter.player.image"
              :alt="currentBatter.player.name"
              class="w-full h-full object-cover"
              @error="handleImageError"
            />
          </div>
        </div>
      </div>
    </div>

    <!-- Empty State -->
    <div
      v-if="!currentBatter && !currentPitcher"
      class="text-center py-12 px-4 bg-gray-50 dark:bg-gray-800 rounded-xl border-2 border-dashed border-gray-300 dark:border-gray-700"
    >
      <div class="w-16 h-16 mx-auto mb-4 bg-gray-200 dark:bg-gray-700 rounded-full flex items-center justify-center">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
        </svg>
      </div>
      <p class="text-gray-500 dark:text-gray-400 font-medium">Waiting for game to start...</p>
      <p class="text-sm text-gray-400 dark:text-gray-500 mt-1">Players will appear here once the game begins.</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { LineupPlayerState } from '~/types/game'

interface Props {
  currentBatter?: LineupPlayerState | null
  currentPitcher?: LineupPlayerState | null
}

const props = withDefaults(defineProps<Props>(), {
  currentBatter: null,
  currentPitcher: null
})

// Handle broken images
const handleImageError = (event: Event) => {
  const img = event.target as HTMLImageElement
  // Fallback to a default player silhouette or hide image
  img.style.display = 'none'
}
</script>

<style scoped>
/* Optional: Add subtle animations */
.current-situation > div {
  animation: fadeIn 0.3s ease-in;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
