<template>
  <div class="page">
    <Header />
    <main class="container">
      <header class="page-header">
        <div>
          <h1>Descubrimiento</h1>
          <p>Últimos cambios detectados en el catálogo.</p>
        </div>
        <div class="filters">
          <label>Días</label>
          <select v-model.number="days" @change="refresh">
            <option :value="3">3</option>
            <option :value="7">7</option>
            <option :value="14">14</option>
            <option :value="30">30</option>
          </select>
        </div>
      </header>

      <div v-if="pending" class="panel">Cargando...</div>
      <div v-else-if="error" class="panel error">Error: {{ error.message }}</div>
      <section v-else class="grid">
        <article v-for="change in changes" :key="change.id" class="card">
          <div class="card-header">
            <div>
              <h3>{{ change.snapshot?.title || 'Sin título' }}</h3>
              <p class="muted">{{ change.snapshot?.publisher || 'Sin publicador' }}</p>
            </div>
            <span class="pill">{{ change.eventType }}</span>
          </div>
          <p class="desc" v-if="change.snapshot?.description">{{ change.snapshot.description }}</p>
          <div class="tags">
            <span v-for="tag in change.tags?.topics || []" :key="tag" class="tag">{{ tag }}</span>
            <span v-for="tag in change.tags?.territories || []" :key="tag" class="tag">{{ tag }}</span>
          </div>
          <div class="card-footer">
            <small>{{ formatDate(change.createdAt) }}</small>
            <a v-if="change.snapshot?.sourceUrl" :href="change.snapshot.sourceUrl" target="_blank">Fuente</a>
          </div>
        </article>
      </section>
    </main>
  </div>
</template>

<script setup>
import Header from '~/components/Header.vue'
const config = useRuntimeConfig()
const days = ref(7)
const { data, pending, error, refresh } = await useFetch(() => `${config.public.apiBase}/discover/changes?days=${days.value}&limit=30`)
const changes = computed(() => data.value ?? [])

function formatDate(value) {
  if (!value) return ''
  return new Date(value).toLocaleString('es-ES')
}
</script>

<style scoped>
.page {
  min-height: 100vh;
  background: #eef2f9;
  font-family: 'Space Grotesk', 'Segoe UI', sans-serif;
}
.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 4rem;
}
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 2rem;
}
.filters {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
select {
  padding: 0.4rem 0.6rem;
  border-radius: 8px;
  border: 1px solid #cbd5f5;
  background: #fff;
}
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1.5rem;
}
.card {
  background: #fff;
  border-radius: 20px;
  padding: 1.5rem;
  box-shadow: 0 14px 32px rgba(15, 23, 42, 0.08);
  display: grid;
  gap: 0.8rem;
}
.card-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
}
.card-header h3 {
  margin: 0;
}
.pill {
  font-size: 0.75rem;
  padding: 0.2rem 0.6rem;
  background: #0f172a;
  color: #fff;
  border-radius: 999px;
  height: fit-content;
}
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.tag {
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  background: #e2e8f0;
  font-size: 0.75rem;
}
.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.85rem;
}
.panel {
  background: #fff;
  border-radius: 16px;
  padding: 1.5rem;
}
.panel.error {
  border: 1px solid #fecaca;
  color: #b91c1c;
}
.muted {
  color: #64748b;
}
.desc {
  color: #475569;
}
</style>
