<script setup lang="ts">
import { ref, computed } from 'vue';
import FixedLayout from '~/components/layouts/FixedLayout.vue';
import MinimalHeader from '~/components/parts/MinimalHeader.vue';

const { locale, t } = useI18n();
const localePath = useLocalePath();

const { data: posts } = useAsyncData('art-posts', async () => {
  const currentLocale = locale.value;

  // Always fetch English articles as the base
  const enPosts = await queryCollection('art')
    .where('path', 'LIKE', `/art/en/%`)
    .order('date', 'DESC')
    .all();

  // If we're already on English, no need for a second query
  if (currentLocale === 'en') return enPosts;

  // Fetch translated articles for the current locale
  const localePosts = await queryCollection('art')
    .where('path', 'LIKE', `/art/${currentLocale}/%`)
    .order('date', 'DESC')
    .all();

  // Build a set of slugs that have a translation
  const translatedSlugs = new Set(
    localePosts.map((p) => p.path.replace(`/art/${currentLocale}/`, ''))
  );

  // Keep English articles that have no translation in the current locale
  const enFallbacks = enPosts.filter(
    (p) => !translatedSlugs.has(p.path.replace('/art/en/', ''))
  );

  // Merge: translated first, then English fallbacks, re-sorted by date
  return [...localePosts, ...enFallbacks].sort(
    (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
  );
}, { watch: [locale, () => useRoute().path] });

const isFallback = (art) => art.path.startsWith('/art/en/') && locale.value !== 'en';

const ART_LIMIT = 6;
const showAll = ref(false);

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

<template>
    <MinimalHeader></MinimalHeader>
    <FixedLayout>
        <Container>
            <h2 class="section-title">ART GALLERY</h2>
            <div class="grid">
                <NuxtLink v-for="art in displayedArt"
                :key="art.slug"
                class="selector"
                :to="isFallback(art) ? `/art/${art.slug}` : localePath(`/art/${art.slug}`)">
                    <span class="selector-border-top" aria-hidden="true">────────</span>
                    <NuxtImg
                        :src="art.thumb"
                        :alt="art.title"
                        class="selector-img"
                        width="600"
                        height="250"
                        fit="cover"
                    />
                    <span class="selector-border-bottom" aria-hidden="true">────────</span>
                    <div class="overlay-label">{{ art.title }}</div>
                </NuxtLink>
            </div>
            <p v-if="posts && posts.length > ART_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>
        </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;
  }
}

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

.grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(250px, 1fr));
    gap: 16px;
    padding: 24px 0;
}

.selector {
    width: 100%;
    height: 250px;
    overflow: hidden;
    position: relative;
    cursor: pointer;
    transition: all 0.1s steps(3, end);
    display: block;
    border: 2px solid var(--color-border-color);
    background-color: var(--color-background-fore);

    &:hover {
        border-color: var(--color-link);
        transform: translateY(-2px);
        box-shadow: 4px -4px 0px 0px var(--color-link);
    }
    
    .selector-border-top,
    .selector-border-bottom {
        position: absolute;
        left: 30px;
        right: 30px;
        height: 0;
        color: var(--color-border-color);
        font-family: 'Hurmit', monospace;
        font-size: 0;
        line-height: 0;
        white-space: nowrap;
        z-index: 10;
        transition: color 0.1s steps(2, end);
    }
    
    .selector-border-top {
        top: -2px;
        &::before { content: "═══════════════════"; }
    }
    
    .selector-border-bottom {
        bottom: -2px;
        &::before { content: "═══════════════════"; }
    }
}

.selector-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.overlay-label {
    position: absolute;
    bottom: -2px;
    left: 30px;
    background-color: var(--color-link);
    color: var(--color-background-fore);
    padding: 2px 8px;
    font-family: 'Hurmit', monospace;
    font-size: 0.7rem;
    letter-spacing: 1px;
    text-transform: uppercase;
    box-shadow: inset 0 -2px 0px 0px rgba(0,0,0,0.3);
    pointer-events: none;
}

.show-more-toggle {
    margin-top: 16px;
    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);
        }
    }
}

@media (max-width: 900px) {
  .grid {
      grid-template-columns: repeat(2, minmax(200px, 1fr));
  }
  
  .selector {
      height: 200px;
  }
}

@media (max-width: 640px) {
  .section-title {
    font-size: 1rem;
  }
  
  .grid {
      grid-template-columns: repeat(1, minmax(200px, 1fr));
      padding: 16px 0;
  }
  
  .selector {
      height: 180px;
  }
}
</style>
