<template>
  <div class="page">
    <Header />
    <main class="container">
      <header class="page-header">
        <div>
          <h1>Catálogo</h1>
          <p>Datasets ingestados desde datos.gob.es con clasificación básica.</p>
        </div>
        <button class="btn" @click="refresh">Actualizar</button>
      </header>

      <div v-if="pending" class="state">Cargando...</div>
      <div v-else-if="error" class="state error">Error: {{ error.message }}</div>
      <section v-else class="grid">
        <article v-for="item in items" :key="item.id" class="card">
          <div class="card-header">
            <h3>{{ item.title }}</h3>
            <span class="pill">{{ item.format || 'Sin formato' }}</span>
          </div>
          <p class="publisher">{{ item.publisher || 'Sin publicador' }}</p>
          <p class="desc" v-if="item.description">{{ item.description }}</p>
          <div class="card-footer">
            <small>Actualizado: {{ formatDate(item.updatedAt) }}</small>
            <a v-if="item.sourceUrl" :href="item.sourceUrl" target="_blank">Fuente</a>
          </div>
        </article>
      </section>
    </main>
  </div>
</template>

<script setup>
import Header from '~/components/Header.vue'
const config = useRuntimeConfig()
const { data, pending, error, refresh } = await useFetch(`${config.public.apiBase}/catalog`)
const items = computed(() => data.value ?? [])

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

<style scoped>
.page {
  background: #f8fafc;
  min-height: 100vh;
  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;
}
.btn {
  padding: 0.6rem 1.4rem;
  border-radius: 999px;
  border: 1px solid #0f172a;
  background: #0f172a;
  color: #fff;
  font-weight: 600;
}
.state {
  padding: 1.5rem;
  background: #fff;
  border-radius: 16px;
}
.state.error {
  border: 1px solid #fecaca;
  color: #b91c1c;
}
.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 12px 30px rgba(15, 23, 42, 0.08);
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}
.card-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: flex-start;
}
.card-header h3 {
  margin: 0;
  font-size: 1.1rem;
}
.pill {
  font-size: 0.75rem;
  padding: 0.2rem 0.6rem;
  background: #e2e8f0;
  border-radius: 999px;
}
.publisher {
  color: #475569;
  font-weight: 600;
}
.desc {
  color: #475569;
  font-size: 0.95rem;
}
.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
  color: #64748b;
}
.card-footer a {
  color: #0f172a;
  font-weight: 600;
}
</style>
