<template>
  <div class="max-w-7xl mx-auto px-4 py-8 md:py-12">
    <h1 class="text-3xl md:text-4xl font-ui font-bold text-sky-text mb-8">
      {{ $t('skills.title') }}
    </h1>

    <!-- Loading state -->
    <div v-if="pending" class="space-y-10">
      <div v-for="i in 4" :key="i">
        <div class="bg-sky-text/5 h-7 rounded w-32 mb-4" />
        <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
          <div v-for="j in 4" :key="j" class="bg-sky-text/5 rounded-xl h-28 animate-pulse" />
        </div>
      </div>
    </div>

    <!-- Error state -->
    <div v-else-if="error" class="text-center py-16">
      <p class="text-sky-text/60 font-narrative mb-6">
        {{ $t('skills.load_error') }}
      </p>
      <button
        class="px-6 py-3 bg-sky-accent text-sky-dark font-ui font-semibold rounded-lg hover:opacity-90 transition-opacity"
        @click="refresh()"
      >
        {{ $t('common.retry') }}
      </button>
    </div>

    <!-- Skills by category -->
    <div v-else class="space-y-12">
      <section
        v-for="(category, categoryIndex) in categories"
        :key="category.category"
        class="category-section"
        :style="{ '--category-delay': `${categoryIndex * 150}ms` }"
      >
        <h2 class="text-xl font-ui font-semibold text-sky-text mb-5 flex items-center gap-2">
          <span class="text-sky-accent">{{ getCategoryIcon(category.category) }}</span>
          {{ category.category_label }}
        </h2>

        <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
          <FeatureSkillCard
            v-for="(skill, skillIndex) in category.skills"
            :key="skill.id"
            :skill="skill"
            class="skill-card-animated"
            :style="{ '--animation-delay': `${categoryIndex * 150 + skillIndex * 60}ms` }"
            @click="handleSkillClick"
          />
        </div>
      </section>

      <!-- Empty state -->
      <div v-if="categories.length === 0" class="text-center py-16">
        <p class="text-sky-text/60 font-narrative">
          {{ $t('skills.no_skills') }}
        </p>
      </div>

      <!-- Placeholder for vis.js skill tree (Epic 3) - Desktop only -->
      <div class="hidden lg:block mt-12 p-8 border-2 border-dashed border-sky-text/10 rounded-xl text-center">
        <p class="text-sky-text/40 font-narrative">
          {{ $t('skills.skill_tree_placeholder') }}
        </p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { Skill } from '~/types/skill'
import { useProgressionStore } from '~/stores/progression'

const { t } = useI18n()
const { setPageMeta } = useSeo()
const store = useProgressionStore()

const { data, pending, error, refresh } = await useFetchSkills()

const categories = computed(() => data.value?.data ?? [])

// SEO
setPageMeta({
  title: t('skills.page_title'),
  description: t('skills.page_description'),
})

// Category icons
const categoryIcons: Record<string, string> = {
  frontend: '🎨',
  backend: '⚙️',
  tools: '🛠️',
  soft_skills: '💡',
}

function getCategoryIcon(category: string): string {
  return categoryIcons[category.toLowerCase()] ?? '📚'
}

// Handle skill click (preparation for Story 2.5)
function handleSkillClick(skill: Skill) {
  // Will be implemented in Story 2.5 - modal with related projects
  console.log('Skill clicked:', skill.slug)
}

onMounted(() => {
  store.visitSection('competences')
})
</script>

<style scoped>
.category-section {
  animation: fadeIn 0.5s ease-out forwards;
  animation-delay: var(--category-delay, 0ms);
  opacity: 0;
}

.skill-card-animated {
  animation: fadeInUp 0.4s ease-out forwards;
  animation-delay: var(--animation-delay, 0ms);
  opacity: 0;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .category-section,
  .skill-card-animated {
    animation: none;
    opacity: 1;
  }
}
</style>
