<?php
session_start();
require_once 'db_config.php';
require_once 'blog_helpers.php';

// Get category by slug
$categorySlug = $_GET['slug'] ?? '';
if (empty($categorySlug)) {
    header('Location: blogs.php');
    exit;
}

$category = getCategoryBySlug($categorySlug, $conn);
if (!$category) {
    header('HTTP/1.0 404 Not Found');
    $notFound = true;
} else {
    $notFound = false;
    
    // Pagination
    $page = $_GET['page'] ?? 1;
    $limit = 9;
    $offset = ($page - 1) * $limit;
    
    // Get filters
    $filters = [
        'category_id' => $category['id'],
        'limit' => $limit,
        'offset' => $offset
    ];
    
    // Get total count
    $totalBlogs = getBlogCount($conn, $filters);
    $totalPages = ceil($totalBlogs / $limit);
    
    // Get blogs
    $blogs = getBlogs($conn, $filters);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    
    <?php if (!$notFound): ?>
        <title>Category: <?php echo htmlspecialchars($category['name']); ?> - Blog</title>
        <meta name="description" content="<?php echo htmlspecialchars($category['description']); ?>">
    <?php else: ?>
        <title>Category Not Found</title>
    <?php endif; ?>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            background: #f5f5f5;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .page-header {
            text-align: center;
            margin-bottom: 40px;
            background: white;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .page-header h1 {
            font-size: 42px;
            color: #2c3e50;
            margin-bottom: 10px;
        }
        
        .page-header p {
            font-size: 16px;
            color: #666;
        }
        
        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #3498db;
            text-decoration: none;
            font-weight: 600;
        }
        
        .back-link:hover {
            text-decoration: underline;
        }
        
        .blogs-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 30px;
            margin-bottom: 40px;
        }
        
        .blog-card {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            transition: all 0.3s;
            text-decoration: none;
            color: #333;
            display: flex;
            flex-direction: column;
        }
        
        .blog-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 4px 16px rgba(0,0,0,0.15);
        }
        
        .blog-card-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
            background: #ecf0f1;
        }
        
        .blog-card-content {
            padding: 20px;
            flex: 1;
            display: flex;
            flex-direction: column;
        }
        
        .blog-card-category {
            display: inline-block;
            background: #3498db;
            color: white;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 11px;
            font-weight: 600;
            margin-bottom: 10px;
            width: fit-content;
        }
        
        .blog-card-title {
            font-size: 18px;
            font-weight: 700;
            margin-bottom: 10px;
            color: #2c3e50;
            line-height: 1.3;
        }
        
        .blog-card-excerpt {
            font-size: 14px;
            color: #666;
            margin-bottom: 15px;
            flex: 1;
        }
        
        .blog-card-meta {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 12px;
            color: #999;
            padding-top: 10px;
            border-top: 1px solid #ecf0f1;
        }
        
        .pagination {
            display: flex;
            justify-content: center;
            gap: 5px;
            flex-wrap: wrap;
        }
        
        .pagination a,
        .pagination span {
            padding: 10px 14px;
            border: 1px solid #ddd;
            border-radius: 4px;
            text-decoration: none;
            color: #3498db;
            display: inline-block;
            transition: all 0.3s;
        }
        
        .pagination a:hover {
            background: #ecf0f1;
        }
        
        .pagination .current {
            background: #3498db;
            color: white;
            border-color: #3498db;
        }
        
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .empty-state h3 {
            color: #2c3e50;
            margin-bottom: 10px;
        }
        
        .empty-state a {
            display: inline-block;
            margin-top: 15px;
            padding: 10px 20px;
            background: #3498db;
            color: white;
            text-decoration: none;
            border-radius: 4px;
        }
        
        .not-found {
            text-align: center;
            padding: 60px 20px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .not-found h1 {
            font-size: 48px;
            color: #e74c3c;
            margin-bottom: 10px;
        }
        
        .not-found a {
            display: inline-block;
            margin-top: 15px;
            padding: 10px 20px;
            background: #3498db;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            margin-right: 10px;
        }
        
        @media (max-width: 768px) {
            .page-header h1 {
                font-size: 32px;
            }
            
            .blogs-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <?php require_once 'header.php'; ?>
    
    <div class="container">
        <?php if ($notFound): ?>
            <div class="not-found">
                <h1>404</h1>
                <p>Category not found.</p>
                <a href="blogs.php">Back to Blogs</a>
                <a href="index.php">Home</a>
            </div>
        <?php else: ?>
            <a href="blogs.php" class="back-link">← Back to All Blogs</a>
            
            <div class="page-header">
                <h1><?php echo htmlspecialchars($category['name']); ?></h1>
                <?php if ($category['description']): ?>
                    <p><?php echo htmlspecialchars($category['description']); ?></p>
                <?php endif; ?>
            </div>
            
            <?php if (!empty($blogs)): ?>
                <!-- Blogs Grid -->
                <div class="blogs-grid">
                    <?php foreach ($blogs as $blog): ?>
                        <a href="blog.php?slug=<?php echo htmlspecialchars($blog['slug']); ?>" class="blog-card">
                            <?php if ($blog['featured_image']): ?>
                                <img src="uploads/blog/<?php echo htmlspecialchars($blog['featured_image']); ?>" alt="<?php echo htmlspecialchars($blog['title']); ?>" class="blog-card-image">
                            <?php endif; ?>
                            <div class="blog-card-content">
                                <span class="blog-card-category">
                                    <?php echo htmlspecialchars($blog['category_name']); ?>
                                </span>
                                <h3 class="blog-card-title">
                                    <?php echo htmlspecialchars($blog['title']); ?>
                                </h3>
                                <p class="blog-card-excerpt">
                                    <?php echo htmlspecialchars(substr($blog['excerpt'] ?? $blog['content'], 0, 150)); ?>...
                                </p>
                                <div class="blog-card-meta">
                                    <span><?php echo date('M d, Y', strtotime($blog['publish_date'] ?? $blog['created_at'])); ?></span>
                                    <span><?php echo $blog['views']; ?> views</span>
                                </div>
                            </div>
                        </a>
                    <?php endforeach; ?>
                </div>
                
                <!-- Pagination -->
                <?php if ($totalPages > 1): ?>
                    <div class="pagination">
                        <?php if ($page > 1): ?>
                            <a href="?slug=<?php echo htmlspecialchars($categorySlug); ?>&page=1">« First</a>
                            <a href="?slug=<?php echo htmlspecialchars($categorySlug); ?>&page=<?php echo $page - 1; ?>">‹ Previous</a>
                        <?php endif; ?>
                        
                        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                            <?php if ($i === (int)$page): ?>
                                <span class="current"><?php echo $i; ?></span>
                            <?php elseif ($i >= $page - 2 && $i <= $page + 2): ?>
                                <a href="?slug=<?php echo htmlspecialchars($categorySlug); ?>&page=<?php echo $i; ?>"><?php echo $i; ?></a>
                            <?php endif; ?>
                        <?php endfor; ?>
                        
                        <?php if ($page < $totalPages): ?>
                            <a href="?slug=<?php echo htmlspecialchars($categorySlug); ?>&page=<?php echo $page + 1; ?>">Next ›</a>
                            <a href="?slug=<?php echo htmlspecialchars($categorySlug); ?>&page=<?php echo $totalPages; ?>">Last »</a>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            <?php else: ?>
                <div class="empty-state">
                    <h3>No Posts in This Category</h3>
                    <p>There are no published posts in this category yet.</p>
                    <a href="blogs.php">View All Blogs</a>
                </div>
            <?php endif; ?>
        <?php endif; ?>
    </div>
    
    <?php require_once 'footer.php'; ?>
</body>
</html>
