<?php
include __DIR__ . "/deepanshu/conn.php";
include __DIR__ . "/deepanshu/functions2.php";
header('Content-Type: application/json; charset=utf-8');

$bearer = explode(" ", $_SERVER['HTTP_AUTHORIZATION'] ?? '');
$author = $bearer[1] ?? '';
$is_jwt_valid = is_jwt_valid($author);
$data_auth = json_decode($is_jwt_valid, true);

if (($data_auth['status'] ?? '') !== 'Success') {
    echo json_encode(['code' => 1, 'msg' => 'Unauthorized']);
    exit;
}

$userId = $data_auth['payload']['id'];

$userRow = $conn->query("SELECT owncode FROM shonu_subjects WHERE id = '$userId'")->fetch_assoc();
$owncode = $userRow['owncode'] ?? '';

// ✅ এই Code দিয়ে যারা Register করেছে, তাদের সংখ্যা গোনা
$countRow = $conn->query("SELECT COUNT(*) as cnt FROM shonu_subjects WHERE code = '" . $conn->real_escape_string($owncode) . "'")->fetch_assoc();
$totalReferrals = $countRow['cnt'] ?? 0;

echo json_encode([
    'code' => 0,
    'owncode' => $owncode,
    'total_referrals' => $totalReferrals
]);
