<?php
/**
 * Rig N Dig - Mobile API v1 / Category List
 */
header('Content-Type: application/json');
require_once '../../../config/config.php';
require_once '../../../includes/functions.php';
require_once '../api_helper.php';

try {
    $categories = Database::fetchAll("SELECT id, name, slug, icon, description FROM categories WHERE is_active = 1 ORDER BY sort_order ASC");
    
    // Add product counts
    foreach ($categories as &$cat) {
        $count = Database::fetchOne("SELECT COUNT(*) as c FROM products WHERE category_id = ? AND status = 'active'", [$cat['id']]);
        $cat['product_count'] = (int)($count['c'] ?? 0);
    }

    apiResponse(true, 'Categories loaded', $categories);
} catch (Exception $e) {
    apiResponse(false, 'Failed to load categories');
}
