<?php

namespace App\Http\Api;

use App\Models\Api;
use Exception;

class fiver
{
    protected $agen;
    protected $token;
    protected $url;

    public function __construct()
    {
        $api = Api::where('nx_status', 1)->first();

        if (!$api) {
            throw new Exception('NX API belum aktif / belum diisi.');
        }

        $this->agen  = $api->nx_agent_code;
        $this->token = $api->nx_token;
        $this->url   = rtrim($api->nx_endpoint, '/');
    }

    /* =========================
     * UTIL
     * ========================= */
    private function cleanAmount($amount): float
    {
        return (float) preg_replace('/[^\d.]/', '', $amount);
    }

    private function request(array $param)
    {
        $response = $this->sg_connect($this->url, $param);

        if (!$response) {
            return [
                'status' => false,
                'message' => 'API tidak merespon'
            ];
        }

        return json_decode($response, true);
    }

    /* =========================
     * USER
     * ========================= */
    public function createUser(string $username)
    {
        return $this->request([
            'method' => 'user_create',
            'agent_code' => $this->agen,
            'agent_token' => $this->token,
            'user_code' => $username,
        ]);
    }

    public function userBalance(string $username)
    {
        return $this->request([
            'method' => 'money_info',
            'agent_code' => $this->agen,
            'agent_token' => $this->token,
            'user_code' => $username,
        ]);
    }

    public function agentBalance()
    {
        return $this->request([
            'method' => 'money_info',
            'agent_code' => $this->agen,
            'agent_token' => $this->token,
        ]);
    }

    /* =========================
     * TRANSAKSI
     * ========================= */
    public function deposit(string $username, $amount)
    {
        return $this->request([
            'method' => 'user_deposit',
            'agent_code' => $this->agen,
            'agent_token' => $this->token,
            'user_code' => $username,
            'amount' => $this->cleanAmount($amount),
        ]);
    }

    public function withdraw(string $username, $amount)
    {
        return $this->request([
            'method' => 'user_withdraw',
            'agent_code' => $this->agen,
            'agent_token' => $this->token,
            'user_code' => $username,
            'amount' => $this->cleanAmount($amount),
        ]);
    }

    public function resetBalance()
    {
        return $this->request([
            'method' => 'user_withdraw_reset',
            'agent_code' => $this->agen,
            'agent_token' => $this->token,
            'all_users' => true,
        ]);
    }

    /* =========================
     * GAME
     * ========================= */
    public function gameList(string $provider)
    {
        return $this->request([
            'method' => 'game_list',
            'agent_code' => $this->agen,
            'agent_token' => $this->token,
            'provider_code' => $provider,
        ]);
    }
    public function gameListV2(string $provider)
{
    return $this->request([
        'method' => 'game_list_v2',
        'agent_code' => $this->agen,
        'agent_token' => $this->token,
        'provider_code' => $provider,
    ]);
}


    public function openGame(string $username, string $gamecode, string $provider)
    {
        return $this->request([
            'method' => 'game_launch',
            'agent_code' => $this->agen,
            'agent_token' => $this->token,
            'user_code' => $username,
            'game_code' => $gamecode,
            'provider_code' => $provider,
            'lang' => 'id',
        ]);
    }

    public function historyPlay(
        string $username,
        string $type,
        string $start,
        string $end,
        int $page = 1,
        int $perPage = 50
    ) {
        return $this->request([
            'method' => 'get_game_log',
            'agent_code' => $this->agen,
            'agent_token' => $this->token,
            'user_code' => $username,
            'game_type' => $type,
            'start' => $start,
            'end' => $end,
            'page' => $page,
            'perPage' => $perPage,
        ]);
    }

    /* =========================
     * CURL CORE
     * ========================= */
    private function sg_connect(string $url, array $payload)
    {
        $ch = curl_init($url);

        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode($payload),
            CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_CONNECTTIMEOUT => 10,
            CURLOPT_TIMEOUT => 20,
        ]);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }
}
