<?php
class Nexus {
    private $agent_code  = '#';
    private $agent_token = '#';
    private $base_url= 'https://api.nexusggr.com';
    private $log_file= __DIR__ . '/nexus.log'; 
    private function writeLog($label, $data) {
        $time = date('Y-m-d H:i:s');
        $msg  = "[$time] [$label] " . (is_array($data) || is_object($data) ? json_encode($data, JSON_PRETTY_PRINT) : $data);
        file_put_contents($this->log_file, $msg . PHP_EOL, FILE_APPEND);
    }

    private function sendRequest($payload) {
        $payload['agent_code']  = $this->agent_code;
        $payload['agent_token'] = $this->agent_token;

        $jsonData = json_encode($payload);

        $this->writeLog('REQUEST', $payload);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->base_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            $error = curl_error($ch);
            $response = json_encode([
                'status' => 0,
                'msg'    => 'CURL_ERROR',
                'detail' => $error
            ]);
            $this->writeLog('CURL_ERROR', $error);
        }

        curl_close($ch);

        $this->writeLog('RESPONSE', $response);

        return $response;
    }

    public function create_member($username) {
        $payload = [
            'method'    => 'user_create',
            'user_code' => $username
        ];
        return $this->sendRequest($payload);
    }

    public function info_member($username) {
        $payload = [
            'method'    => 'money_info',
            'user_code' => $username
        ];
        return $this->sendRequest($payload);
    }

    public function info_agent() {
        $payload = [
            'method' => 'money_info'
        ];
        return $this->sendRequest($payload);
    }

    public function deposit_member($username, $amount) {
        $payload = [
            'method'     => 'user_deposit',
            'user_code'  => $username,
            'amount'     => (int)$amount,
            'agent_sign' => uniqid()
        ];
        return $this->sendRequest($payload);
    }

    public function withdraw_member($username, $amount) {
        $payload = [
            'method'     => 'user_withdraw',
            'user_code'  => $username,
            'amount'     => (int)$amount,
            'agent_sign' => uniqid()
        ];
        return $this->sendRequest($payload);
    }

    public function reset_balance($username) {
        $payload = [
            'method'    => 'user_withdraw_reset',
            'user_code' => $username
        ];
        return $this->sendRequest($payload);
    }

    public function launch_game($username, $provider_code, $game_code, $lang = 'id', $title = null) {
        $payload = [
            'method'        => 'game_launch',
            'user_code'     => $username,
            'provider_code' => $provider_code,
            'game_code'     => $game_code,
            'lang'          => $lang,
        ];
        return $this->sendRequest($payload);
    }

    public function get_game_history($username, $startDate, $endDate, $page = 0, $perPage = 1000) {
        $payload = [
            'method'    => 'get_game_log',
            'user_code' => $username,
            'game_type' => 'slot',
            'start'     => $startDate,
            'end'       => $endDate,
            'page'      => $page,
            'perPage'   => $perPage,
        ];
        return $this->sendRequest($payload);
    }

    public function get_provider() {
        $payload = [
            'method' => 'provider_list'
        ];
        return $this->sendRequest($payload);
    }

    public function get_games($provider_code) {
        $payload = [
            'method'        => 'game_list',
            'provider_code' => $provider_code
        ];
        return $this->sendRequest($payload);
    }
}

$nx = new Nexus();
?>
