<?php
/**
 * Admin API — Get All Categories
 */
session_start();
header('Content-Type: application/json');

if (empty($_SESSION['admin_logged_in'])) {
    http_response_code(401);
    echo json_encode(['status' => 'error', 'message' => 'Unauthorized']);
    exit;
}

require_once '../../includes/config.php';

try {
    $stmt = $pdo->query("SELECT id, name, slug, created_at FROM categories ORDER BY name ASC");
    $categories = $stmt->fetchAll();

    echo json_encode([
        'status' => 'success',
        'count'  => count($categories),
        'data'   => $categories
    ]);
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>
