<?php

namespace App\Services;

use App\Http\Integrations\Imake\BulkClixAPI;
use App\Http\Integrations\Imake\Requests\BulkClix\MomoPay;
use App\Http\Integrations\Imake\Requests\BulkClix\SendOTP;
use App\Http\Integrations\Imake\Requests\BulkClix\VerifyOTP;
use App\Http\Integrations\Imake\Requests\BulkClix\CheckTStatus;

class BulkClixService
{
    protected $bulkClixAPI;
    /**
     * Create a new class instance.
     */
    public function __construct(BulkClixAPI $bulkClixAPI)
    {
        $this->bulkClixAPI = $bulkClixAPI;
    }

    public function initiatePaymentWithOTP($paymentNumber)
    {
        $request = new SendOTP($paymentNumber);
        $response = $this->bulkClixAPI->send($request);

        if ($response->ok()) {
            return $response->json();
        } else {
            return null;
        }
    }

    public function verifyOTP($paymentNumber, $requestId, $otp)
    {
        $request = new VerifyOTP($paymentNumber, $requestId, $otp);
        $response = $this->bulkClixAPI->send($request);

        if ($response->ok()) {
            return $response->json();
        } else {
            return null;
        }
    }

    public function sendPaymentPrompt($paymentNumber, $amount, $network, $transactionId)
    {
        $payload = [
            'amount' => $amount,
            'phone_number' => $paymentNumber,
            'network' => $network,
            'transaction_id' => $transactionId,
        ];

        $request = new MomoPay($payload);
        $response = $this->bulkClixAPI->send($request);

        if ($response->ok()) {
            return $response->json();
        } else {
            return null;
        }
    }

    public function checkTransactionStatus($transactionId)
    {
        $request = new CheckTStatus($transactionId);
        $response = $this->bulkClixAPI->send($request);

        if ($response->ok()) {
            $status = $response->json()['data']['status'];
            $isSuccess = $status == 'success';
            return $isSuccess;
        } else {
            return null;
        }
    }
}