<script setup lang="ts">
import type { AchievementDef } from '@engine/types'
import { useI18n } from '@/composables/useI18n'

const { t } = useI18n()

defineProps<{
  definitions: AchievementDef[]
  unlockedIds: Set<string>
}>()

const emit = defineEmits<{
  close: []
}>()
</script>

<template>
  <div class="ach-overlay" @click.self="emit('close')" @keydown.escape="emit('close')">
    <div class="ach-panel">
      <h2 class="ach-title">{{ t('ui.achievements') }}</h2>

      <div class="ach-list">
        <div
          v-for="ach in definitions"
          :key="ach.id"
          class="ach-card"
          :class="{ locked: !unlockedIds.has(ach.id), hidden: ach.hidden && !unlockedIds.has(ach.id) }"
        >
          <div class="ach-icon">
            <img v-if="ach.icon" :src="ach.icon" class="ach-img" />
            <span v-else class="ach-star">{{ unlockedIds.has(ach.id) ? '⭐' : '🔒' }}</span>
          </div>
          <div class="ach-body">
            <div class="ach-title-text">
              {{ !unlockedIds.has(ach.id) && ach.hidden ? '???' : t(ach.titleKey || ach.title) }}
            </div>
            <div class="ach-desc-text">
              {{ !unlockedIds.has(ach.id) && ach.hidden ? '' : t(ach.descKey || ach.description) }}
            </div>
          </div>
        </div>
      </div>

      <button class="ach-close" @click="emit('close')">{{ t('ui.back') }}</button>
    </div>
  </div>
</template>

<style scoped>
.ach-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.ach-panel {
  background: rgba(12, 12, 18, 0.85);
  border: none;
  border-radius: 12px;
  padding: 36px 40px;
  min-width: 420px;
  max-width: 520px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.ach-title {
  text-align: center;
  font-size: 22px;
  font-weight: 400;
  color: #c9a84c;
  letter-spacing: 3px;
  margin-bottom: 24px;
}

.ach-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  padding-right: 8px;
}

.ach-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 6px;
}

.ach-card.locked {
  opacity: 0.4;
}

.ach-card.hidden {
  opacity: 0.2;
}

.ach-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.ach-star {
  font-size: 20px;
}

.ach-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.ach-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.ach-title-text {
  font-size: 14px;
  color: #c9a84c;
}

.ach-desc-text {
  font-size: 12px;
  color: #888;
}

.ach-close {
  margin-top: 20px;
  padding: 10px 36px;
  font-size: 14px;
  color: #888;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  align-self: center;
}

.ach-close:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #ccc;
}

.ach-close:focus-visible {
  outline: 2px solid #c9a84c;
  outline-offset: 2px;
}
</style>
