---
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 category IDs or categories
  const uniqueCategories = new Set<string>();
  
  Object.values(allPosts).forEach((post: any) => {
    // Prioritize categoryId as route identifier
    if (post.frontmatter?.categoryId) {
      const categoryIds = Array.isArray(post.frontmatter.categoryId) 
        ? post.frontmatter.categoryId 
        : [post.frontmatter.categoryId];
      
      categoryIds.forEach((categoryId: string) => {
        if (categoryId) uniqueCategories.add(categoryId.toLowerCase());
      });
    }
    // If no categoryId, use category as fallback
    else if (post.frontmatter?.category) {
      const categories = Array.isArray(post.frontmatter.category) 
        ? post.frontmatter.category 
        : [post.frontmatter.category];
      
      categories.forEach((category: string) => {
        if (category) uniqueCategories.add(category.toLowerCase());
      });
    }
  });
  
  // Generate paths for each category
  return Array.from(uniqueCategories).map(category => ({
    params: { category: encodeURIComponent(category) },
    props: { category }
  }));
}

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

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

const { category } = Astro.params;
const decodedCategory = category ? decodeURIComponent(category) : '';

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

// Find category name matching the current category ID
let categoryName = decodedCategory;

// Try to find matching category name from all posts
Object.values(allPosts).forEach((post: any) => {
  // Check categoryId
  if (post.frontmatter?.categoryId) {
    const categoryIds = Array.isArray(post.frontmatter.categoryId) 
      ? post.frontmatter.categoryId 
      : [post.frontmatter.categoryId];
    
    const matchingCategoryIdIndex = categoryIds.findIndex((id: string) => 
      id.toLowerCase() === decodedCategory.toLowerCase()
    );
    
    if (matchingCategoryIdIndex !== -1 && post.frontmatter.category) {
      // If matching categoryId is found and post has category attribute
      const categories = Array.isArray(post.frontmatter.category) 
        ? post.frontmatter.category 
        : [post.frontmatter.category];
      
      // Ensure categories array length matches categoryIds
      if (categories.length > matchingCategoryIdIndex) {
        categoryName = categories[matchingCategoryIdIndex];
      }
    }
  }
});

// Page title and description
const title = `${categoryName} - Blog | Joey Z.`;
const description = `Explore articles about ${categoryName}. Dive into my thoughts on ${categoryName} and related topics.`;
---

<TaxonomyPageLayout
  lang={lang}
  title={title}
  description={description}
  taxonomyType="category"
  currentTaxonomy={decodedCategory}
  displayName={categoryName}
  posts={sortedBlogPosts}
  categories={categories}
  tags={tags}
/>

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