<?php
include 'connectAPI.php';

class API {

    private $user_agent;
    private $signature;
    private $base_url = "https://api.nexusggr.com";

    public function __construct($user_agent, $signature) {
        $this->user_agent = $user_agent;
        $this->signature = $signature;
    }

    /**
     * Fungsi pembentuk data dasar API
     */
    private function postdata($method, $additional_data = []) {
        $base_data = [
            'agent_code' => $this->user_agent,
            'agent_token' => $this->signature,
            'method' => $method
        ];
        return array_merge($base_data, $additional_data);
    }

    /**
     * Ambil saldo agent
     */
    public function money_info() {
        $postdata = $this->postdata('money_info');
        return $this->send_request($postdata);
    }

    /**
     * Ambil saldo user tertentu
     */
    public function money_info_user($user_code) {
        $postdata = $this->postdata('money_info', [
            'user_code' => strval($user_code)
        ]);
        return $this->send_request($postdata);
    }

    /**
     * Membuat user di NexusGGR
     */
    public function user_create($user_code) {
        $postdata = $this->postdata('user_create', [
            'user_code' => strval($user_code)
        ]);
        return $this->send_request($postdata);
    }

    /**
     * Deposit saldo ke user di NexusGGR
     */
    public function user_deposit($user_code, $amount) {
        $postdata = $this->postdata('user_deposit', [
            'user_code' => strval($user_code),
            'amount' => floatval($amount)
        ]);
        return $this->send_request($postdata);
    }

    /**
     * Tarik saldo user dari NexusGGR
     */
    public function user_withdraw($user_code, $amount) {
        $postdata = $this->postdata('user_withdraw', [
            'user_code' => strval($user_code),
            'amount' => floatval($amount)
        ]);
        return $this->send_request($postdata);
    }

    /**
     * Luncurkan game untuk user
     */
    public function game_launch($user_code, $provider_code, $game_code, $lang = 'en') {
        $postdata = $this->postdata('game_launch', [
            'user_code' => strval($user_code),
            'provider_code' => $provider_code,
            'game_code' => $game_code,
            'lang' => $lang
        ]);
        return $this->send_request($postdata);
    }

    /**
     * Ambil daftar provider
     */
    public function provider_list() {
        $postdata = $this->postdata('provider_list');
        return $this->send_request($postdata);
    }

    /**
     * Ambil daftar game berdasarkan provider
     */
    public function game_list($provider_code) {
        $postdata = $this->postdata('game_list', [
            'provider_code' => $provider_code
        ]);
        return $this->send_request($postdata);
    }

    /**
     * Ambil riwayat taruhan (bet history)
     */
    public function history_bet() {
        $currentDate = date('Y-m-d');
        $postdata = $this->postdata('get_game_log', [
            'game_type' => 'slot',
            'start' => $currentDate . ' 00:00:00',
            'end' => $currentDate . ' 23:59:59',
            'page' => 0,
            'perPage' => 1000
        ]);
        return $this->send_request($postdata);
    }

    /**
     * Fungsi utama untuk kirim request ke API NexusGGR
     */
    private function send_request($postdata) {
        $jsonData = json_encode($postdata);
        $headers = ['Content-Type: application/json'];

        $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, $headers);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Simpan cookie session sementara (jika dibutuhkan API)
        curl_setopt($ch, CURLOPT_COOKIEFILE, '');
        curl_setopt($ch, CURLOPT_COOKIEJAR, '');

        $res = curl_exec($ch);

        if ($res === false) {
            $error = curl_error($ch);
            curl_close($ch);
            return [
                'status' => 0,
                'msg' => 'CURL_ERROR',
                'detail' => $error
            ];
        }

        curl_close($ch);

        $decoded = json_decode($res, true);

        // Validasi JSON response
        if (json_last_error() !== JSON_ERROR_NONE) {
            return [
                'status' => 0,
                'msg' => 'JSON_DECODE_ERROR',
                'detail' => json_last_error_msg()
            ];
        }

        return $decoded;
    }
}

// Inisialisasi objek global
$FLASHERDEV = new API($user_agent, $signature);
?>
