<?php

class GSPay {
    private $auth_key = "7513227ac5314a6d93d3480312e60285";
    private $secret_key = "fb4d6df6c6994cc183736252128c350f";
    private $base_url = "https://api.thegspay.com/v2/integrations";

    public function __construct($auth_key = null, $secret_key = null) {
        if ($auth_key) $this->auth_key = $auth_key;
        if ($secret_key) $this->secret_key = $secret_key;
    }

    public function generatePaymentIDR($transaction_id, $player_username, $amount, $channel = null) {
        $url = $this->base_url . "/operators/" . $this->auth_key . "/idr/payment";
        
        /* Rumus Signature: transaction_id + player_username + amount + secret_key */
        $signature = md5($transaction_id . $player_username . $amount . $this->secret_key);

        $params = [
            'transaction_id' => $transaction_id,
            'player_username' => $player_username,
            'amount' => (int)$amount,
            'signature' => $signature
        ];

        if ($channel) $params['channel'] = $channel;

        return $this->postRequest($url, $params);
    }

    public function getPaymentStatus($transaction_id) {
        $url = $this->base_url . "/operators/" . $this->auth_key . "/idr/getpayment";
        
        $params = [
            'auth_key' => $this->auth_key,
            'transaction_id' => $transaction_id
        ];
        
        return $this->getRequest($url, $params);
    }

    public function makePayout($transaction_id, $player_username, $acc_name, $acc_number, $amount, $bank_target, $description = "") {
        $url = $this->base_url . "/operators/" . $this->auth_key . "/idr/payout";
        
        /* Rumus Signature Payout: transaction_id + player_username + amount + account_number + secret_key */
        $signature = md5($transaction_id . $player_username . $amount . $acc_number . $this->secret_key);

        $params = [
            'transaction_id' => $transaction_id,
            'player_username' => $player_username,
            'account_name' => $acc_name,
            'account_number' => $acc_number,
            'amount' => (int)$amount,
            'bank_target' => $bank_target,
            'trx_description' => $description ?: $transaction_id,
            'signature' => $signature
        ];

        return $this->postRequest($url, $params);
    }

    public function getPayoutStatus($transaction_id) {
        $url = $this->base_url . "/operators/" . $this->auth_key . "/idr/payout/status";
        
        $params = [
            'auth_key' => $this->auth_key,
            'transaction_id' => $transaction_id
        ];

        return $this->getRequest($url, $params);
    }

    public function getBalance() {
        $url = "https://api.thegspay.com/v2/integrations/operator/" . $this->auth_key . "/get/balance";
        return $this->getRequest($url);
    }

    public function generatePaymentUSDT($transaction_id, $player_username, $amount) {
        $url = $this->base_url . "/operators/" . $this->auth_key . "/cryptocurrency/trc20/usdt";
        
        /* USDT Signature menggunakan format 2 desimal pada amount */
        $amount_formatted = number_format($amount, 2, '.', '');
        $signature = md5($transaction_id . $player_username . $amount_formatted . $this->secret_key);

        $params = [
            'transaction_id' => $transaction_id,
            'player_username' => $player_username,
            'amount' => $amount_formatted,
            'signature' => $signature
        ];

        return $this->postRequest($url, $params);
    }

    public function validateCallbackSignature($data, $type = 'payment') {
        $amount_formatted = number_format($data['amount'], 2, '.', '');
        
        if ($type === 'payment') {
            /* Formula: idrpayment_id + amount(2 dec) + transaction_id + status + secret_key */
            $raw = $data['idrpayment_id'] . $amount_formatted . $data['transaction_id'] . $data['status'] . $this->secret_key;
        } elseif ($type === 'payout') {
            /* Formula: idrpayout_id + account_number + amount(2 dec) + transaction_id + secret_key */
            $raw = $data['idrpayout_id'] . $data['account_number'] . $amount_formatted . $data['transaction_id'] . $this->secret_key;
        } elseif ($type === 'usdt') {
            /* Formula: cryptopayment_id + amount(2 dec) + transaction_id + status + secret_key */
            $raw = $data['cryptopayment_id'] . $amount_formatted . $data['transaction_id'] . $data['status'] . $this->secret_key;
        } else {
            return false;
        }
        
        return md5($raw) === $data['signature'];
    }

    private function postRequest($url, $data) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        $response = curl_exec($ch);
        curl_close($ch);
        return json_decode($response, true);
    }

    private function getRequest($url, $params = []) {
        if (!empty($params)) {
            $url .= '?' . http_build_query($params);
        }
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);
        return json_decode($response, true);
    }
}