<?php
/**
 * Admin API — Get All Users
 * Returns all users as JSON (admin-only endpoint).
 */

session_start();

header('Content-Type: application/json');

// Guard: Only allow admins
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, full_name, phone_number, email, role, created_at FROM users ORDER BY role DESC, created_at DESC");
    $users = $stmt->fetchAll();

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