<?php
// شروع session قبل از هر چیز
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once '../config.php';

if (!isAdmin()) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false]);
    exit;
}

$rep_id = (int)($_GET['id'] ?? 0);

if ($rep_id === 0) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false]);
    exit;
}

$rep = $pdo->prepare("
    SELECT r.*, 
           p.name as panel_name,
           (SELECT COUNT(*) FROM reseller_users WHERE representative_id = r.id) as total_users,
           (SELECT SUM(total_traffic) FROM reseller_users WHERE representative_id = r.id) as total_user_traffic
    FROM representatives r 
    JOIN panels p ON r.panel_id = p.id 
    WHERE r.id = ?
");
$rep->execute([$rep_id]);
$rep = $rep->fetch();

header('Content-Type: application/json');
echo json_encode([
    'success' => true,
    'data' => $rep
]);
?>
