<?php
require_once 'db.php';

$limit = 25;
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$offset = ($page - 1) * $limit;

$stmt = $pdo->prepare("
    SELECT u.user_id, u.first_name, u.last_name, u.email, COUNT(r.user_id) AS referral_count 
    FROM users u 
    LEFT JOIN users r ON u.user_id = r.referrer_id 
    WHERE COALESCE(u.is_demo, 0) = 0
    GROUP BY u.user_id 
    HAVING referral_count > 0
    ORDER BY referral_count DESC 
    LIMIT :limit OFFSET :offset
");

$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);

// ✅ Removed strategy_url logic

echo json_encode($users);
?>
