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

export async function getStaticPaths() {
  const allPosts = await getCollection('posts');
  
  // Get all unique tags from all posts
  const uniqueTags = [...new Set(allPosts.flatMap(post => post.data.tags || []))];
  
  // Create a page for each tag
  return uniqueTags.map(tag => {
    // Filter posts that have this tag
    const filteredPosts = allPosts.filter(post => 
      post.data.tags && post.data.tags.includes(tag)
    );
    
    return {
      params: { tag },
      props: { posts: filteredPosts, tag },
    };
  });
}

const { posts, tag } = Astro.props;

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

<BaseLayout title={`Posts tagged with "${tag}" | LaForce IT Blog`} description={`Articles and guides related to ${tag}`}>
  <main class="container">
    <section class="tag-header">
      <h1 class="tag-title">Posts tagged with <span>#{tag}</span></h1>
      <p class="tag-description">
        Browse all {sortedPosts.length} articles related to this topic
      </p>
      <a href="/tags" class="tag-link">View all tags</a>
    </section>

    <div class="blog-grid">
      {sortedPosts.map((post) => (
        <article class="post-card">
          {post.data.heroImage ? (
            <img
              width={720}
              height={360}
              src={post.data.heroImage}
              alt=""
              class="post-image"
            />
          ) : (
            <img
              width={720}
              height={360}
              src="/images/placeholders/default.jpg"
              alt=""
              class="post-image"
            />
          )}
          <div class="post-content">
            <div class="post-meta">
              <time datetime={post.data.pubDate ? new Date(post.data.pubDate).toISOString() : ''}>
                {post.data.pubDate ? new Date(post.data.pubDate).toLocaleDateString('en-us', {
                  year: 'numeric',
                  month: 'short',
                  day: 'numeric',
                }) : 'No date'}
              </time>
              {post.data.category && (
                <span class="post-category">
                  {post.data.category}
                </span>
              )}
            </div>
            <h3 class="post-title">
              <a href={`/posts/${post.slug}/`}>{post.data.title}</a>
              {post.data.draft && <span class="draft-badge">Draft</span>}
            </h3>
            <p class="post-excerpt">{post.data.description}</p>
            <div class="post-footer">
              <span class="post-read-time">{post.data.readTime || '5 min read'}</span>
              <a href={`/posts/${post.slug}/`} class="read-more">Read More</a>
            </div>
          </div>
        </article>
      ))}
    </div>
  </main>
</BaseLayout>

<style>
.tag-header {
  margin: 3rem 0;
  text-align: center;
}

.tag-title {
  font-size: clamp(1.8rem, 4vw, 2.5rem);
  margin-bottom: 1rem;
}

.tag-title span {
  background: linear-gradient(90deg, var(--accent-primary), var(--accent-tertiary));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-family: 'JetBrains Mono', monospace;
}

.tag-description {
  color: var(--text-secondary);
  font-size: 1.1rem;
  margin-bottom: 1rem;
}

.tag-link {
  display: inline-block;
  margin-top: 1rem;
  color: var(--accent-primary);
  text-decoration: none;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.9rem;
  border-bottom: 1px dashed var(--accent-primary);
  transition: all 0.3s ease;
}

.tag-link:hover {
  border-bottom: 1px solid var(--accent-primary);
}

.draft-badge {
  display: inline-block;
  margin-left: 0.5rem;
  padding: 0.25rem 0.5rem;
  background-color: rgba(226, 232, 240, 0.2);
  color: #94a3b8;
  font-size: 0.75rem;
  border-radius: 0.25rem;
  vertical-align: middle;
}

/* Include styles from blog index if needed, like .blog-grid, .post-card etc. */
.blog-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 2rem;
  margin: 2rem 0 4rem;
}

.post-card {
  background: var(--card-bg);
  border-radius: 10px;
  border: 1px solid var(--card-border);
  overflow: hidden;
  transition: all 0.3s ease;
  position: relative;
  z-index: 1;
}

.post-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 30px rgba(6, 182, 212, 0.1);
  border-color: rgba(56, 189, 248, 0.4);
}

.post-card::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(6, 182, 212, 0.05), rgba(139, 92, 246, 0.05));
  z-index: -1;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.post-card:hover::before {
  opacity: 1;
}

.post-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-bottom: 1px solid var(--card-border);
}

.post-content {
  padding: 1.5rem;
}

.post-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.post-category {
  background: rgba(6, 182, 212, 0.1);
  color: var(--accent-primary);
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.75rem;
}

.post-title {
  font-size: 1.25rem;
  margin-bottom: 0.75rem;
  line-height: 1.3;
}

.post-title a {
  color: var(--text-primary);
  text-decoration: none;
  transition: color 0.3s ease;
}

.post-title a:hover {
  color: var(--accent-primary);
}

.post-excerpt {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-bottom: 1.5rem;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.post-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.read-more {
  color: var(--accent-primary);
  text-decoration: none;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  transition: color 0.3s ease;
}

.read-more:hover {
  color: var(--accent-secondary);
}

.read-more::after {
  content: '→';
}
</style>