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

// Get all posts
const allPosts = await getCollection('posts');

// Get all unique tags with their counts
const tagMap = new Map();
allPosts.forEach(post => {
  if (post.data.tags) {
    post.data.tags.forEach(tag => {
      if (tagMap.has(tag)) {
        tagMap.set(tag, tagMap.get(tag) + 1);
      } else {
        tagMap.set(tag, 1);
      }
    });
  }
});

// Convert the map to an array of objects and sort by count (descending)
const sortedTags = Array.from(tagMap.entries())
  .map(([tag, count]) => ({ tag, count }))
  .sort((a, b) => b.count - a.count);
---

<BaseLayout title="All Tags | LaForce IT Blog" description="Browse all topics and tags available on LaForce IT Blog">
  <main class="container">
    <section class="tags-header">
      <h1 class="tags-title">Browse by <span>Tag</span></h1>
      <p class="tags-description">
        Explore all topics covered in the blog, from Kubernetes to home lab setups and automation
      </p>
    </section>

    <div class="tags-container">
      {sortedTags.map(({ tag, count }) => (
        <a href={`/tag/${tag}`} class="tag-card">
          <div class="tag-name">#{tag}</div>
          <div class="tag-count">{count} {count === 1 ? 'article' : 'articles'}</div>
        </a>
      ))}
    </div>
  </main>
</BaseLayout>

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

.tags-title {
  font-size: clamp(2rem, 5vw, 3.5rem);
  margin-bottom: 1rem;
}

.tags-title span {
  background: linear-gradient(90deg, var(--accent-primary), var(--accent-tertiary));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.tags-description {
  color: var(--text-secondary);
  font-size: clamp(1rem, 2vw, 1.2rem);
  max-width: 700px;
  margin: 0 auto;
}

.tags-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1.5rem;
  margin: 3rem 0;
}

.tag-card {
  background: var(--card-bg);
  border-radius: 0.75rem;
  border: 1px solid var(--card-border);
  padding: 1.5rem;
  text-decoration: none;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  position: relative;
  overflow: hidden;
}

.tag-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;
}

.tag-card:hover {
  transform: translateY(-5px);
  border-color: rgba(56, 189, 248, 0.4);
}

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

.tag-name {
  font-family: 'JetBrains Mono', monospace;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
}

.tag-count {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

@media (max-width: 768px) {
  .tags-container {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }
}
</style> 