<script setup lang="ts">
import { CDRAGON_BASE, mapPath } from '~/utils/cdragon'

const props = defineProps<{
  keystoneId: number
  itemId: number
  keystore: Map<number, Perk>
  itemMap: Map<number, Item>
  pickrate: number
  selected: boolean
  index: number
}>()

const emit = defineEmits<{
  select: [index: number]
}>()
</script>

<template>
  <div class="build-variant-selector">
    <div :class="['build-variant-card', { selected }]" @click="emit('select', index)">
      <div class="variant-content">
        <!-- Keystone -->
        <NuxtImg
          v-if="keystoneId && props.keystore.get(keystoneId)"
          class="variant-keystone"
          :src="CDRAGON_BASE + mapPath(props.keystore.get(keystoneId)!.iconPath)"
        />
        <!-- First core item -->
        <NuxtImg
          v-if="itemMap.get(itemId)"
          class="variant-item"
          :src="CDRAGON_BASE + mapPath(itemMap.get(itemId)!.iconPath)"
        />
        <div v-else class="variant-item-placeholder" />
      </div>
      <span class="variant-pickrate">{{ (pickrate * 100).toFixed(1) }}%</span>
    </div>
  </div>
</template>

<style scoped>
.build-variant-selector {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 20px;
  padding: 0 12px;
  overflow-x: auto;
  scrollbar-width: none;
}

.build-variant-selector::-webkit-scrollbar {
  display: none;
}

.build-variant-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 14px;
  background: var(--color-surface-darker);
  border: 2px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 90px;
}

.build-variant-card:hover {
  border-color: var(--color-on-surface);
}

.build-variant-card.selected {
  border-color: #4a9eff;
  background: linear-gradient(135deg, rgba(74, 158, 255, 0.15), rgba(74, 158, 255, 0.05));
}

.variant-content {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 4px;
}

.variant-keystone {
  width: 28px;
  height: 28px;
  border-radius: 50%;
}

.variant-item {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  border: 1px solid rgba(183, 184, 225, 0.3);
}

.variant-item-placeholder {
  width: 28px;
  height: 28px;
  background: var(--color-surface);
  border-radius: 4px;
  border: 1px solid rgba(183, 184, 225, 0.3);
}

.variant-pickrate {
  font-size: 0.75rem;
  color: var(--color-on-surface);
  opacity: 0.8;
}

/* Responsive: Mobile */
@media only screen and (max-width: 900px) {
  .build-variant-card {
    min-width: 70px;
    padding: 6px 10px;
  }

  .variant-keystone,
  .variant-item {
    width: 24px;
    height: 24px;
  }

  .variant-item-placeholder {
    width: 24px;
    height: 24px;
  }

  .variant-pickrate {
    font-size: 0.65rem;
  }
}

@media only screen and (max-width: 400px) {
  .build-variant-selector {
    gap: 6px;
  }

  .build-variant-card {
    min-width: 60px;
    padding: 5px 8px;
  }

  .variant-keystone,
  .variant-item {
    width: 20px;
    height: 20px;
  }

  .variant-item-placeholder {
    width: 20px;
    height: 20px;
  }
}
</style>
