<?php


namespace App\Services\MobileMoney\MTN;

use App\Models\PaymentManagement\Payment;
use Bmatovu\MtnMomo\Products\Collection;
use Illuminate\Support\Facades\DB;

trait MTNMobileMoneyTrait{

    /*numero pour les différents tests fourni par l'api*/
    protected  $tests_number = [
        '46733123450', // (failed) 1
        '46733123451', // (rejected) 2
        '46733123452', //  3 (ongoing (will answer pending first and if requested again after 30 seconds it will respond success))
        '46733123454', // (pending) 4
        '46733123453', // (timeout)
    ];

    /*codes erreur d'echec de transaction*/
    protected $raisons = [
        "EXPIRED", // (failed)
        "APPROVAL_REJECTED", // (failed)

        "INTERNAL_PROCESSING_ERROR", // (failed)
        "RESOURCE_NOT_FOUND", // (failed)
    ];

    private $momoCollection;

    public function mtn_momo_init_parameters(){
        $this->momoCollection = new Collection();
    }

    /*resultat est le reponse de verification de statut*/
    public function mtnMomoCheckStatus($resultat){

        $response = [
            "transaction" => [
                "success" => false,
                "message" => "",
                "stop_check_statut" => true,
                "message_non_formater" => "",
                "failed" => false,
            ]
        ];

        $transaction_status = $resultat['status'] ?? null;

        $errorCode= $resultat['reason'] ?? '';

        $possibles_statuses = [
            "PENDING" => [
                "desc" => __("En attente de validation.")
            ],
            "TIMEOUT" => [
                "desc" => __("Le temps de validation de votre paiement a expiré.")
            ],
            "REJECTED" => [
                "desc" => __("La transaction a été brutalement rejété")
            ],

            "FAILED" => [
                "desc" => __("Votre transaction a été annulé. Raisons possibles:"),
                "possibles-reasons" => [
                    __("Votre solde est insuffisant ,"),
                    __("Vous avez trop perdu de temps pour approuver la transaction."),
                ]
            ],

            "SUCCESSFUL" => [
                "desc" => __("Payment reçu")." "
            ],
        ];

        if (!empty($transaction_status)){

            /*si le statut retourné est parmis les statuts possibles*/
            if(array_key_exists($transaction_status,$possibles_statuses)){

                // si le paiement a reuissi ou a été validé
                if ($transaction_status == "SUCCESSFUL"){
                    $response['transaction']['success'] = true;
                    $response['transaction']['montant'] = $resultat['amount'];
                    $response['transaction']['reference'] = $resultat['financialTransactionId'];
                    $response['transaction']['message'] = $possibles_statuses[$transaction_status]['desc'];
                    $response['transaction']['message_non_formater'] = $response['transaction']['message'];

                }

                elseif ($transaction_status == "PENDING"){
                    $response['transaction']['message'] = "";
                    $response['transaction']['stop_check_statut'] = false;

                }/*si le statut est different de success ou pending*/
                else{
                    $response['transaction']['failed'] = true;

                    $messageErreur = $possibles_statuses[$transaction_status]['desc']."<br> ".
                        \App\Libs\Facades\Utilities::debug($possibles_statuses[$transaction_status]['possibles-reasons'] ?? [])
                        .__("code")." : <b>$code_erreur</b>";

                    $response['transaction']['message_non_formater'] = $messageErreur;
                    $response['transaction']['message'] = ExtendedFunctionnalities::alert(['type'=>"error","msg"=>$messageErreur]);
                }
            }else{
                $messageErreur = __("le statut renvoyé par le fournisseur est inconnu a l'application");
                $response['transaction']['message'] = ExtendedFunctionnalities::alert(['type'=>"error","msg"=>$messageErreur]);
                $response['transaction']['message_non_formater'] = $messageErreur;
            }
        }else{
            $messageErreur = __("le fournisseur n'a pas répondu correctement.");
            $response['transaction']['message'] = ExtendedFunctionnalities::alert(['type'=>"error","msg"=>$messageErreur]);
            $response['transaction']['message_non_formater'] = $messageErreur;

        }
        return $response;
    }

}
