<script setup lang="ts">
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import {
  BUILTIN_PLAYERS,
  playerAvatarUrl,
  getPlayerDisplayName,
  formatPlayerMeta,
  getPlayerSearchTokens,
} from '@thebet365/shared';

const props = defineProps<{
  modelValue: string | null;
}>();

const emit = defineEmits<{
  'update:modelValue': [value: string | null];
}>();

const { t, locale } = useI18n();
const keyword = ref('');

const filtered = computed(() => {
  const q = keyword.value.trim().toLowerCase();
  if (!q) return BUILTIN_PLAYERS;
  return BUILTIN_PLAYERS.filter((p) => getPlayerSearchTokens(p).some((token) => token.includes(q)));
});

function select(key: string) {
  emit('update:modelValue', props.modelValue === key ? null : key);
}
</script>

<template>
  <div class="picker">
    <div class="picker-head">
      <label class="picker-label">{{ t('profile.avatar') }}</label>
      <input
        v-model="keyword"
        type="search"
        class="picker-search"
        :placeholder="t('profile.avatar_search')"
      />
    </div>

    <div class="picker-grid">
      <button
        v-for="player in filtered"
        :key="player.id"
        type="button"
        class="picker-item"
        :class="{ active: modelValue === player.id }"
        @click="select(player.id)"
      >
        <img
          :src="playerAvatarUrl(player.id) ?? ''"
          :alt="getPlayerDisplayName(player, locale)"
          class="picker-photo"
        />
        <span class="picker-name">{{ getPlayerDisplayName(player, locale) }}</span>
        <span class="picker-meta">{{ formatPlayerMeta(player, locale) }}</span>
      </button>
    </div>

    <p v-if="!filtered.length" class="picker-empty">{{ t('profile.avatar_empty') }}</p>
  </div>
</template>

<style scoped>
.picker {
  margin-bottom: 12px;
}

.picker-head {
  margin-bottom: 10px;
}

.picker-label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 6px;
}

.picker-search {
  width: 100%;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #0a0a0a;
  color: var(--text);
  font-size: 13px;
}

.picker-search:focus {
  outline: none;
  border-color: var(--border-gold-soft);
}

.picker-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  max-height: 280px;
  overflow-y: auto;
  padding-right: 2px;
}

.picker-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 4px 6px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #0a0a0a;
  cursor: pointer;
  min-width: 0;
}

.picker-item.active {
  border-color: var(--border-gold-soft);
  box-shadow: 0 0 0 1px rgba(212, 175, 55, 0.18);
  background: rgba(212, 175, 55, 0.08);
}

.picker-photo {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  object-fit: cover;
  object-position: top;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

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

.picker-meta {
  width: 100%;
  font-size: 9px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.picker-empty {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
}
</style>
