<?php
include "config.php";

function goMaintenance()
{
    header("Location: ../../m/index.php?pesan=31");
    exit;
}
function goerror()
{
    header("Location: ../../m/index.php?pesan=35");
    exit;
}

function nexusRequest($params)
{
    global $apiConfig;

    $payload = json_encode($params, JSON_UNESCAPED_SLASHES);

    $ch = curl_init($apiConfig['endpoint']);
    curl_setopt_array($ch, [
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $payload,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 30,
        CURLOPT_HTTPHEADER     => [
            'Content-Type: application/json',
            'Content-Length: ' . strlen($payload)
        ],
    ]);

    $response = curl_exec($ch);

    if ($response === false) {
        $err = curl_error($ch);
        curl_close($ch);

        // DEBUG LOG ERROR
        file_put_contents(__DIR__.'/nexus_debug.log', 
            "=== CURL ERROR ===\n".$err."\n\n", 
            FILE_APPEND
        );

        return [
            'status' => 0,
            'msg'    => $err
        ];
    }

    curl_close($ch);

    // DEBUG LOG RESPONSE
    file_put_contents(__DIR__.'/nexus_debug.log', 
        "=== REQUEST ===\n".print_r($params, true).
        "\n=== RESPONSE RAW ===\n".$response.
        "\n=====================\n\n", 
        FILE_APPEND
    );

    return json_decode($response, true);
}

function getBetHistory($user_code, $game_type, $start, $end, $page = 0){
    global $apiConfig;

    return nexusRequest([
        'method'      => 'get_game_log',
        'agent_code'  => $apiConfig['agent_code'],
        'agent_token' => $apiConfig['agent_token'],
        'user_code'   => $user_code,
        'game_type'   => $game_type,
        'start'       => $start,
        'end'         => $end,
        'page'        => $page,
        'perPage'     => 1000
    ]);
}

/* =====================================================
   CREATE USER
===================================================== */
function nexusCreateUser($userCode)
{
    global $apiConfig;

    return nexusRequest([
        'method'      => 'create_user',
        'agent_code'  => $apiConfig['agent_code'],
        'agent_token' => $apiConfig['agent_token'],
        'user_code'   => preg_replace('/[^a-zA-Z0-9]/', '', $userCode)
    ]);
}

/* =====================================================
   TRANSFER IN (DEPOSIT)
===================================================== */
function nexusTransferIn($userCode, $amount, $trxid)
{
    global $apiConfig;

    $amount = intval($amount);
    if ($amount < 1) {
        return [
            'status' => 0,
            'msg' => 'INVALID AMOUNT'
        ];
    }

    return nexusRequest([
        'method'      => 'user_deposit',
        'agent_code'  => $apiConfig['agent_code'],
        'agent_token' => $apiConfig['agent_token'],
        'user_code'   => $userCode,
        'amount'      => $amount,
        'agent_sign'  => $trxid
    ]);
}

/* =====================================================
   TRANSFER OUT (WITHDRAW)
===================================================== */
function nexusTransferOut($userCode, $amount, $trxid)
{
    global $apiConfig;

    if (!is_numeric($amount) || $amount <= 0) {
        return [
            'status' => 0,
            'msg'    => 'SKIP WITHDRAW',
            'detail' => 'amount invalid'
        ];
    }

    return nexusRequest([
        'method'      => 'user_withdraw',
        'agent_code'  => $apiConfig['agent_code'],
        'agent_token' => $apiConfig['agent_token'],
        'user_code'   => $userCode,
        'amount'      => $amount,
        'agent_sign'  => $trxid
    ]);
}

/* =====================================================
   OPEN GAME (TANPA AMOUNT - SESUAI DOC)
===================================================== */
function nexusOpenGame($userCode, $providerCode, $gameCode, $lang = 'id')
{
    global $apiConfig;

    return nexusRequest([
        'method'        => 'game_launch',
        'agent_code'    => $apiConfig['agent_code'],
        'agent_token'   => $apiConfig['agent_token'],
        'user_code'     => $userCode,
        'provider_code' => strtoupper($providerCode),
        'game_code'     => $gameCode,
        'lang'          => $lang
    ]);
}

/* =====================================================
   AGENT / USER BALANCE
===================================================== */
function nexusAgentBalance($userCode = '')
{
    global $apiConfig;

    $data = [
        'method'      => 'money_info',
        'agent_code'  => $apiConfig['agent_code'],
        'agent_token' => $apiConfig['agent_token']
    ];

    if ($userCode !== '') {
        $data['user_code'] = $userCode;
    }

    return nexusRequest($data);
}

function nexusGetGameList($provider)
{
    global $apiConfig;

    return nexusRequest([
        'method'        => 'game_list',
        'agent_code'    => $apiConfig['agent_code'],
        'agent_token'   => $apiConfig['agent_token'],
        'provider_code' => strtoupper($provider),
    ]);
}


function nexusGetProviderList()
{
    global $apiConfig;

    return nexusRequest([
        'method'      => 'provider_list',
        'agent_code'  => $apiConfig['agent_code'],
        'agent_token' => $apiConfig['agent_token'],
    ]);
}