<?php
error_reporting(E_ALL);
$host="localhost"; 
$user="jrdufvyeyv_megacuan"; 
$password="jrdufvyeyv_megacuan"; 
$database="jrdufvyeyv_megacuan"; 
$conn=mysqli_connect($host,$user,$password,$database);
//cek koneksi 
if($conn){ 
//echo "berhasil koneksi"; 
}else{ 
	echo "gagal koneksi"; 
} 

class CXFCORE {
    private $key;
    private $endpoint;
    private $domain;

    public function __construct($token, $endpoint) {
        $this->key = $token;
        $this->endpoint = rtrim($endpoint, '/'); // hapus trailing slash
        $this->domain = $_SERVER['HTTP_HOST'] ?? 'localhost';
    }

    // Create / register player
    public function registerGame($extplayer) {
        $url = $this->endpoint . '?create';
        $payload = [
            'player' => $extplayer,
            'verify' => $this->key,
            'domain' => $this->domain
        ];
        return $this->sendRequest($url, 'POST', $payload);
    }

    // Deposit balance
    public function depositBalance($extplayer, $nominal) {
        $url = $this->endpoint . '?deposit';
        $payload = [
            'player' => $extplayer,
            'balance' => $nominal,
            'verify' => $this->key,
            'domain' => $this->domain
        ];
        return $this->sendRequest($url, 'POST', $payload);
    }

    // Withdraw balance
    public function withdrawBalance($extplayer, $nominal) {
        $url = $this->endpoint . '?withdraw';
        $payload = [
            'player' => $extplayer,
            'balance' => $nominal,
            'verify' => $this->key,
            'domain' => $this->domain
        ];
        return $this->sendRequest($url, 'POST', $payload);
    }

    // Get balance
    public function getBalance($extplayer) {
        $url = $this->endpoint . '?wallet&' . http_build_query([
            'player' => $extplayer,
            'verify' => $this->key,
            'domain' => $this->domain
        ]);
        return $this->sendRequest($url, 'GET');
    }

    // Send request internal
    private function sendRequest($url, $method = 'POST', $data = []) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

        if ($method === 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        } elseif ($method === 'GET') {
            curl_setopt($ch, CURLOPT_HTTPGET, true);
        }

        $response = curl_exec($ch);
        if ($response === false) {
            curl_close($ch);
            return ['status' => 0, 'message' => curl_error($ch), 'data' => []];
        }

        curl_close($ch);

        $decoded = json_decode($response, true);
        // Pastikan selalu ada status & data
        if (!isset($decoded['status'])) $decoded['status'] = 0;
        if (!isset($decoded['data'])) $decoded['data'] = [];
        if (!isset($decoded['message'])) $decoded['message'] = 'No message';
        return $decoded;
    }
}

$sql_255 = mysqli_query($conn, "SELECT * FROM `tb_apigames` WHERE cuid = 1") or die(mysqli_error($conn));
$s255 = mysqli_fetch_array($sql_255);
            
// Panggil API untuk register user eksternal
$token = $s255['token'];
$owner = $s255['owner'];
$endpoint = $s255['endpoint'];

$qristoked = "TESTERI";
$endpoint = 'https://rest.cxfcore.sbs';
$kingisback = new CXFCORE($token, $endpoint);
?>