<?php
require_once __DIR__ . '/../../cors.php'; // Enable CORS for React Native
// GET /api/admin/users — list all customers with order count
require_once __DIR__ . '/../../../config/database.php';
require_once __DIR__ . '/../../../middleware/auth.php';

requireAdmin();

if ($_SERVER['REQUEST_METHOD'] !== 'GET') jsonError('Method not allowed', 405);

$db   = getDB();
$users = $db->query(
    'SELECT u.id, u.name, u.email, u.phone, u.address, u.role, u.created_at,
            COUNT(o.id) AS order_count
     FROM users u
     LEFT JOIN orders o ON o.user_id = u.id
     GROUP BY u.id
     ORDER BY u.created_at DESC'
)->fetchAll();

jsonResponse($users);
