---
import TaxonomyPageLayout from '../../../layouts/TaxonomyPageLayout.astro';
import { type Lang } from '@/types/i18n';
import { defaultLang } from '@/i18n/ui';
import { getTaxonomyPageData } from '@/utils/blog-utils';

// Generate static paths for dynamic routing
export async function getStaticPaths() {
  const allPosts = await import.meta.glob('../posts/*.md', { eager: true });
  
  // Collect all tags
  const uniqueTags = new Set<string>();
  
  Object.values(allPosts).forEach((post: any) => {
    // Prioritize tagId as route identifier
    if (post.frontmatter?.tagId && Array.isArray(post.frontmatter.tagId)) {
      post.frontmatter.tagId.forEach((tagId: string) => {
        if (tagId) uniqueTags.add(tagId.toLowerCase());
      });
    }
    // If no tagId, use tags as fallback
    else if (post.frontmatter?.tags && Array.isArray(post.frontmatter.tags)) {
      post.frontmatter.tags.forEach((tag: string) => {
        if (tag) uniqueTags.add(tag.toLowerCase());
      });
    }
  });
  
  // Generate paths for each tag
  return Array.from(uniqueTags).map(tag => ({
    params: { tag: encodeURIComponent(tag) },
    props: { tag }
  }));
}

// Get current language environment
const lang = Astro.currentLocale as Lang || defaultLang;

// Get current tag (from URL parameters)
export interface Props {
  tag: string;
}

const { tag } = Astro.params;
const decodedTag = tag ? decodeURIComponent(tag) : '';

// Get taxonomy page data using the utility function
const { posts: sortedBlogPosts, categories, tags, allPosts } = 
  await getTaxonomyPageData(lang, { tag: decodedTag });

// Find tag name matching the current tag ID
let tagName = decodedTag;

// Try to find matching tag name from all posts
Object.values(allPosts).forEach((post: any) => {
  // Check tagId
  if (post.frontmatter?.tagId && Array.isArray(post.frontmatter.tagId) && 
      post.frontmatter?.tags && Array.isArray(post.frontmatter.tags)) {
    // Find matching index between tagId and tag name
    const tagIndex = post.frontmatter.tagId.findIndex((id: string) => 
      id.toLowerCase() === decodedTag.toLowerCase()
    );
    
    if (tagIndex !== -1 && tagIndex < post.frontmatter.tags.length) {
      tagName = post.frontmatter.tags[tagIndex];
    }
  }
});

// If no matching tag name is found, use tag ID and format it (capitalize first letter)
if (tagName === decodedTag) {
  tagName = decodedTag.charAt(0).toUpperCase() + decodedTag.slice(1);
}

// Generate page title and description
const title = `#${tagName} - Blog | Joey Z.`;
const description = `Explore articles tagged with #${tagName}. Dive into my thoughts on ${tagName} and related topics.`;
---

<TaxonomyPageLayout
  lang={lang}
  title={title}
  description={description}
  taxonomyType="tag"
  currentTaxonomy={decodedTag}
  displayName={tagName}
  posts={sortedBlogPosts}
  categories={categories}
  tags={tags}
/>

<style>
  .line-clamp-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
</style>