---
// src/pages/tag/[tag].astro
// Dynamic route for tag pages

import BaseLayout from '../../layouts/BaseLayout.astro';
import { getCollection } from 'astro:content';

export async function getStaticPaths() {
  const allPosts = await getCollection('blog');
  const uniqueTags = [...new Set(allPosts.map((post) => post.data.tags).flat())];

  return uniqueTags.map((tag) => {
    const filteredPosts = allPosts.filter((post) => post.data.tags.includes(tag));
    return {
      params: { tag },
      props: { posts: filteredPosts },
    };
  });
}

const { tag } = Astro.params;
const { posts } = Astro.props;

// Format date
const formatDate = (dateStr) => {
  const date = new Date(dateStr);
  return date.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
};

// Sort posts by date (newest first)
const sortedPosts = posts.sort((a, b) => {
  const dateA = new Date(a.data.pubDate);
  const dateB = new Date(b.data.pubDate);
  return dateB.getTime() - dateA.getTime();
});
---

<BaseLayout title={`Posts tagged with "${tag}" | LaForce IT Blog`} description={`Articles and guides related to ${tag}`}>
  <div class="container tag-page">
    <header class="tag-hero">
      <h1>Posts tagged with <span class="tag-highlight">{tag}</span></h1>
      <p>Explore {sortedPosts.length} {sortedPosts.length === 1 ? 'article' : 'articles'} related to {tag}</p>
    </header>

    <div class="posts-grid">
      {sortedPosts.map((post) => (
        <article class="post-card">
          <!-- Simplified image rendering that works reliably -->
          <img
            width={720}
            height={360}
            src={post.data.heroImage || "/images/placeholders/default.jpg"}
            alt=""
            class="post-image"
          />
          <div class="post-content">
            <time datetime={post.data.pubDate}>{formatDate(post.data.pubDate)}</time>
            <h2 class="post-title">
              <a href={`/posts/${post.slug}/`}>{post.data.title}</a>
            </h2>
            <p class="post-excerpt">{post.data.description}</p>
            <div class="post-meta">
              <span class="reading-time">{post.data.minutesRead || '5 min'} read</span>
              <ul class="post-tags">
                {post.data.tags.map((tagName) => (
                  <li>
                    <a href={`/tag/${tagName}`} class={tagName === tag ? 'current-tag' : ''}>
                      {tagName}
                    </a>
                  </li>
                ))}
              </ul>
            </div>
          </div>
        </article>
      ))}
    </div>

    <a href="/tags" class="all-tags-link">
      <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <line x1="19" y1="12" x2="5" y2="12"></line>
        <polyline points="12 19 5 12 12 5"></polyline>
      </svg>
      View all tags
    </a>
  </div>
</BaseLayout>

<style>
  .tag-page {
    padding-top: 2rem;
    padding-bottom: 4rem;
  }
  
  .tag-hero {
    text-align: center;
    margin-bottom: 3rem;
    animation: fadeIn 0.5s ease-out;
  }
  
  @keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
  }
  
  .tag-hero h1 {
    font-size: var(--font-size-3xl);
    margin-bottom: 0.5rem;
    line-height: 1.2;
  }
  
  .tag-highlight {
    background: linear-gradient(90deg, var(--accent-primary), var(--accent-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    color: transparent;
    font-weight: 700;
  }
  
  .tag-hero p {
    color: var(--text-secondary);
    font-size: var(--font-size-lg);
  }
  
  .posts-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 2rem;
    margin-bottom: 3rem;
  }
  
  .post-card {
    background: var(--card-bg);
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid var(--border-primary);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    animation: fadeIn 0.5s ease-out forwards;
    animation-delay: calc(var(--animation-order, 0) * 0.1s);
    opacity: 0;
    display: flex;
    flex-direction: column;
  }
  
  .post-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    border-color: var(--accent-primary);
  }
  
  .post-image {
    width: 100%;
    height: 200px;
    object-fit: cover;
    border-bottom: 1px solid var(--border-primary);
  }
  
  .post-content {
    padding: 1.5rem;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
  }
  
  .post-content time {
    color: var(--text-tertiary);
    font-size: var(--font-size-sm);
    font-family: var(--font-mono);
  }
  
  .post-title {
    font-size: var(--font-size-xl);
    margin: 0.5rem 0 1rem;
    line-height: 1.3;
  }
  
  .post-title a {
    color: var(--text-primary);
    text-decoration: none;
    transition: color 0.2s ease;
  }
  
  .post-title a:hover {
    color: var(--accent-primary);
  }
  
  .post-excerpt {
    color: var(--text-secondary);
    font-size: var(--font-size-md);
    margin-bottom: 1.5rem;
    line-height: 1.6;
    flex-grow: 1;
  }
  
  .post-meta {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-top: auto;
  }
  
  .reading-time {
    color: var(--text-tertiary);
    font-size: var(--font-size-sm);
    font-family: var(--font-mono);
  }
  
  .post-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    list-style: none;
    padding: 0;
  }
  
  .post-tags li a {
    display: block;
    padding: 0.25rem 0.75rem;
    background: rgba(56, 189, 248, 0.1);
    border-radius: 20px;
    color: var(--accent-primary);
    font-size: var(--font-size-xs);
    text-decoration: none;
    transition: all 0.2s ease;
  }
  
  .post-tags li a:hover {
    background: rgba(56, 189, 248, 0.2);
    transform: translateY(-2px);
  }
  
  .post-tags li a.current-tag {
    background: var(--accent-primary);
    color: var(--bg-primary);
  }
  
  .all-tags-link {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0 auto;
    padding: 0.75rem 1.5rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-primary);
    border-radius: 30px;