<?php
/**
 * GET /api/categories/list.php
 * Public endpoint - returns active categories for the product form dropdown.
 * Pass ?include_inactive=1 (admin-only) to also see deactivated categories,
 * for the admin category-management screen.
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/response.php';
require_once __DIR__ . '/../includes/auth.php';

$pdo = Database::connect();

$includeInactive = false;
if (!empty($_GET['include_inactive'])) {
    start_secure_session();
    if (current_account_type() === 'admin') {
        $includeInactive = true;
    }
}

$sql = 'SELECT id, parent_id, name, is_active FROM categories'
     . ($includeInactive ? '' : ' WHERE is_active = 1')
     . ' ORDER BY name ASC';
$stmt = $pdo->query($sql);
$categories = $stmt->fetchAll();

send_json(true, 'OK', ['categories' => $categories]);
