<script setup lang="ts">
import TableHeader from '~/components/parts/TableHeader.vue';
import FixedLayout from '~/components/layouts/FixedLayout.vue';
import { ref, computed } from 'vue';
import { useSeo } from '~/composables/seo'
const { locale } = useI18n();
const { t } = useI18n();
const localePath = useLocalePath()

useSeo({
  title: 'Blog',
  description: 'Read blog posts by Aran Roig on software engineering, web development, digital art, and creative coding.',
  canonicalUrl: '/blog'
});

// WebPage structured data for blog listing
useHead({
  script: [{
    type: 'application/ld+json',
    innerHTML: JSON.stringify({
      '@context': 'https://schema.org',
      '@type': 'CollectionPage',
      headline: 'Blog Posts by Aran Roig',
      description: 'Read blog posts by Aran Roig on software engineering, web development, digital art, and creative coding.',
      url: 'https://aranroig.com/blog',
      author: {
        '@type': 'Person',
        name: 'Aran Roig'
      },
      inLanguage: ['en', 'es', 'ca']
    })
  }]
});

const {data: posts, refresh} = useAsyncData('blog-posts', async () => 
await queryCollection(`blog`).where('path', 'LIKE', `/blog/${locale.value}/%`).order('date', 'DESC').all()
, {watch: [locale, () => useRoute().path]});

onActivated(() => {
  refresh();
});

const BLOG_LIMIT = 5;
const showAll = ref(false);

const displayedPosts = computed(() => {
  const allPosts = posts.value || [];
  if (showAll.value || allPosts.length <= BLOG_LIMIT) return allPosts;
  return allPosts.slice(0, BLOG_LIMIT);
});
</script>

<template>
    <div class="no-sprite">
      <TableHeader></TableHeader>
    </div>
    <FixedLayout>
      <Container>
        <h1 style="position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;">Blog — Aran Roig</h1>
        <section class="blog-section">
          <h2 class="section-title">{{ t('pages.blog_heading') }}</h2>
          <ul class="tui-list">
            <li v-for="post in displayedPosts" :key="post.slug" class="blog-entry">
              <NuxtLink class="entry-link" :to="localePath({ name: 'blog-slug', params: { slug: post.slug } })">
                <span class="entry-title">{{ post.title }}</span>
              </NuxtLink>
              <span class="entry-meta">[{{ post.date }}] {{ post.description }}</span>
            </li>
          </ul>
           <p v-if="posts && posts.length > BLOG_LIMIT" class="show-more-toggle">
             <button type="button" class="tui-link" @click="showAll = !showAll">{{ showAll ? t('pages.show_less') : t('pages.show_more') }}</button>
           </p>
        </section>
      </Container>
    </FixedLayout>
    <Footer></Footer>
</template>

<style lang="scss" scoped>
.section-title {
  font-family: 'Hurmit', monospace;
  color: var(--color-text);
  font-size: 1.2rem;
  letter-spacing: 0.5px;
  margin: 28px 0 16px 0;
  padding-left: 12px;
  line-height: 1.3;

  &::before {
    content: "├";
    color: var(--color-link);
    margin-right: 8px;
    font-weight: normal;
  }

  &::after {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    background-color: var(--color-link);
    margin-left: 10px;
    vertical-align: middle;
    box-shadow: 0 0 4px var(--color-link);
    animation: blink-cursor 1s steps(1) infinite;
  }
}

.tui-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.blog-entry {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 16px;
  border-left: 2px solid var(--color-border-color);
  margin-bottom: 4px;
  transition: all 0.1s steps(2, end);

  &:hover {
    border-left-color: var(--color-link);
    background-color: var(--color-hover);
  }

  &::before {
    display: none;
  }
}

.entry-link {
  text-decoration: none;
  color: var(--color-text);

  &:hover {
    color: var(--color-link);
    text-shadow: 0 0 6px var(--color-link);
  }
}

.entry-title {
  font-family: 'Hurmit', monospace;
  font-size: 1rem;
  transition: all 0.1s steps(2, end);
}

.entry-meta {
  font-family: 'Hurmit', monospace;
  color: var(--color-text);
  font-size: 0.8rem;
  opacity: 0.6;
}

.show-more-toggle {
  margin-top: 12px;
  padding-left: 14px;

  button {
    background: none;
    border: none;
    color: var(--color-link);
    font-family: 'Hurmit', monospace;
    font-size: 0.85rem;
    cursor: pointer;
    text-shadow: 0 0 4px var(--color-link);
    padding: 0;

    &:hover {
      text-shadow: 0 0 10px var(--color-link), 0 0 3px var(--color-link);
    }
  }
}

@keyframes blink-cursor {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

@media screen and (max-width: 600px) {
  .section-title {
    font-size: 1rem;
  }
  
  .blog-entry {
    padding: 6px 12px;
  }
  
  .entry-title {
    font-size: 0.9rem;
  }
}
</style>

<style lang="scss">
.no-sprite .undertable-wrapper {
  display: none;
}
</style>
