<?php

namespace App\Services;

use GuzzleHttp\Client;

class ApiService
{
    private $client;
    private $baseApi;
    private $agentCode;
    private $signature;

    public function __construct()
    {
        $this->client = new Client();
        $this->baseApi = config('api.base_api');
        $this->agentCode = config('api.agent_code');
        $this->signature = config('api.signature');
    }

    public function createMember($username)
    {
        $endpoint = $this->baseApi . "?cmd=create&agent_code=" . $this->agentCode . "&username=$username&signature=" . $this->signature;
        return $this->sendRequest($endpoint);
    }

    public function getBalances($username)
    {
        $endpoint = $this->baseApi . "?cmd=getbalance&agent_code=" . $this->agentCode . "&username=$username&signature=" . $this->signature;
        return $this->sendRequest($endpoint);
    }

    public function transaksi($username, $type, $amount)
    {
        $endpoint = $this->baseApi . "?cmd=transaksi&agent_code=" . $this->agentCode . "&username=$username&type=$type&amount=$amount&signature=" . $this->signature;
        return $this->sendRequest($endpoint);
    }

    public function playGame($username, $gameId)
    {
        $endpoint = $this->baseApi . "?cmd=launch&agent_code=" . $this->agentCode . "&username=$username&gameid=$gameId&signature=" . $this->signature;
        return $this->sendRequest($endpoint);
    }

    private function sendRequest($endpoint)
    {
        $response = $this->client->get($endpoint);
        return json_decode($response->getBody()->getContents(), true);
    }
}