<?php
session_start();
header('Content-Type: application/json');

// ১. লগইন এবং মাল্টি-টেন্যান্ট সেশন চেক
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true || !isset($_SESSION['isp_id'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized Access']);
    exit;
}

include 'config.php';
include 'routeros_api.class.php';

$current_isp_id = $_SESSION['isp_id'];
$customer_id = $_GET['customer_id'] ?? '';

if (empty($customer_id)) {
    echo json_encode(['success' => false, 'message' => 'Customer ID missing']);
    exit;
}

// ২. ডাটা আইসোলেশন ও সিকিউরিটি চেক: এই কাস্টমারটি বর্তমান আইএসপি-র কিনা তা যাচাই করা
$customer_id_clean = mysqli_real_escape_string($conn, $customer_id);
$customer_check = mysqli_query($conn, "SELECT id FROM customers WHERE customer_id = '$customer_id_clean' AND isp_id = '$current_isp_id' LIMIT 1");

if (mysqli_num_rows($customer_check) === 0) {
    echo json_encode(['success' => false, 'message' => 'Access Denied: Customer not found in your workspace']);
    exit;
}

// ৩. ডাটাবেস থেকে বর্তমান আইএসপির মাইক্রোটিক রাউটার সেটিংস তুলে আনা
$settings_res = mysqli_query($conn, "SELECT * FROM isp_settings WHERE isp_id = '$current_isp_id' LIMIT 1");
$isp_settings = mysqli_fetch_assoc($settings_res);

if (!$isp_settings) {
    echo json_encode(['success' => false, 'message' => 'MicroTik configurations missing for your ISP']);
    exit;
}

$m_ip   = $isp_settings['mikrotik_ip']; 
$m_user = $isp_settings['mikrotik_user']; 
$m_pass = $isp_settings['mikrotik_pass']; 
$m_port = $isp_settings['mikrotik_port'];

$api = new RouterosAPI();

if ($api->connect($m_ip, $m_user, $m_pass, $m_port)) {
    
    $interface_name = $customer_id;
    
    // ইন্টারফেসের সঠিক নাম চেক (ডিরেক্ট customer_id অথবা <pppoe-customer_id> ফরম্যাট)
    $check_interface = $api->comm("/interface/print", array(
        "?name" => $customer_id
    ));

    if (empty($check_interface)) {
        $check_pppoe = $api->comm("/interface/print", array(
            "?name" => "<pppoe-" . $customer_id . ">"
        ));
        if (!empty($check_pppoe)) {
            $interface_name = "<pppoe-" . $customer_id . ">";
        }
    }

    // ট্রাফিক মনিটর করা
    $traffic = $api->comm("/interface/monitor-traffic", array(
        "interface" => $interface_name,
        "once" => ""
    ));
    
    $api->disconnect();

    if (!empty($traffic) && isset($traffic[0])) {
        // bps থেকে Mbps এ রূপান্তর
        $rx_bps = isset($traffic[0]['rx-bits-per-second']) ? (float)$traffic[0]['rx-bits-per-second'] : 0;
        $tx_bps = isset($traffic[0]['tx-bits-per-second']) ? (float)$traffic[0]['tx-bits-per-second'] : 0;

        // Rx = Upload, Tx = Download
        $upload_mbps = $rx_bps / 1048576;
        $download_mbps = $tx_bps / 1048576;

        echo json_encode([
            'success' => true,
            'upload' => number_format($upload_mbps, 2),
            'download' => number_format($download_mbps, 2)
        ]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Interface traffic not found or user offline']);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'Failed to connect with your MikroTik router']);
}