<?php
declare(strict_types=1);

require_once __DIR__ . '/../../../security/auth_guard.php';
require_once __DIR__ . '/../../../config/database.php';
require_once __DIR__ . '/../../../core/Response.php';

$db = Database::getInstance();

/**
 * Fetch categories with blog count (many-to-many)
 */
$stmt = $db->prepare("
    SELECT 
        c.id,
        c.name,
        c.slug,
        c.status,
        c.created_at,
        COUNT(bcm.blog_id) AS blog_count
    FROM blog_categories c
    LEFT JOIN blog_category_map bcm 
        ON bcm.category_id = c.id
    GROUP BY c.id
    ORDER BY c.id DESC
");

$stmt->execute();
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);

Response::success('Blog categories fetched successfully', $categories);
