<?php


namespace App\Services\MobileMoney\MTN;


use App\Models\PaymentManagement\PaymentMethod;
use Bmatovu\MtnMomo\Exceptions\CollectionRequestException;
use Bmatovu\MtnMomo\Products\Collection;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use Ramsey\Uuid\Uuid;

class MTNMobileMoneyCollection extends Collection {

    public function __construct(PaymentMethod $paymentMethod = null,$headers = [], $middleware = [], ClientInterface $client = null){
        parent::__construct($headers, $middleware, $client);

        if ($paymentMethod){
            $config = $paymentMethod->api_config;
            $active = $config['active'] ?? false;
            if ($config and $active){
                if (isset($config['subscriptionKey'])){
                    $this->subscriptionKey = $config['subscriptionKey'];
                }
                if (isset($config['clientId'])){
                    $this->clientId = $config['clientId'];
                }
                if (isset($config['clientSecret'])){
                    $this->clientSecret = $config['clientSecret'];
                }
                if (isset($config['clientCallbackUri'])){
                    $this->clientCallbackUri = $config['clientCallbackUri'];
                }
              //  $api = ["active"=> true,'subscriptionKey' => "subscriptionKey" ,"clientId" => "clientId" ,  "clientSecret" => "clientSecret"];
            }
        }

//        dd($this->subscriptionKey);


    }

    /**
     * Request payee to pay.
     *
     * @see https://momodeveloper.mtn.com/docs/services/collection/operations/requesttopay-POST Documentation
     *
     * @param  string $transactionId Your transaction reference ID, Say: order number.
     * @param  string $partyId Account holder. Usually phone number if type is MSISDN.
     * @param  int $amount How much to debit the payer.
     * @param  string $payerMessage Payer transaction history message.
     * @param  string $payeeNote Payee transaction history message.
     *
     * @throws \Bmatovu\MtnMomo\Exceptions\CollectionRequestException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Exception
     *
     * @return string                Auto generated payment reference. Format: UUID
     */
    public function requestToPay($transactionId, $partyId, $amount, $payerMessage = '', $payeeNote = '')
    {
        $momoTransactionId = Uuid::uuid4()->toString();

        $headers = [
            'X-Reference-Id' => $momoTransactionId,
            'X-Target-Environment' => $this->environment,
//            'X-Callback-Url' => $this->clientCallbackUri,
        ];

        try {
            $this->client->request('POST', $this->transactionUri, [
                'headers' => $headers,
                'json' => [
                    'amount' => $amount,
                    'currency' => $this->currency,
                    'externalId' => $transactionId,
                    'payer' => [
                        'partyIdType' => $this->partyIdType,
                        'partyId' => $partyId,
                    ],
                    'payerMessage' => alphanumeric($payerMessage),
                    'payeeNote' => alphanumeric($payeeNote),
                ],
            ]);

            return $momoTransactionId;
        } catch (RequestException $ex) {
            throw new CollectionRequestException('Request to pay transaction - unsuccessful.', 0, $ex);
        }
    }
}
