<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Http\Resources\TransactionResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\Modules\Transactions\Processors\CreatePaymentTransactionProcessor;
use App\Classes\Modules\Transactions\Processors\ReleaseGoodsToCustomerProcessor;
use App\Classes\ValueObjects\Constants\ApprovalStatus;

class CreatePaymentTransactionLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Create Payment Transactions',
            'message' => 'You have successfully created currency supplier transactions'
        ];
    }

    /** @var FetchesTransaction */
    private $fetchesTransaction;

    /** @var CreatePaymentTransactionProcessor */
    private $createPaymentTransactionProcessor;

    /** @var ReleaseGoodsToCustomerProcessor */
    private $releaseGoodsToCustomerProcessor;

    public function __construct(
        FetchesTransaction $fetchesTransaction,
        CreatePaymentTransactionProcessor $createPaymentTransactionProcessor,
        ReleaseGoodsToCustomerProcessor $releaseGoodsToCustomerProcessor
    )
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->createPaymentTransactionProcessor = $createPaymentTransactionProcessor;
        $this->releaseGoodsToCustomerProcessor = $releaseGoodsToCustomerProcessor;
    }

    public function logic(Request $request) : JsonResponse
    {
        $payment_method = PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')];

        $invoice_transaction = $this->fetchesTransaction->execute(['id' => $request->input('transaction_id')]);

        $payment_transaction = $this->createPaymentTransactionProcessor->execute($invoice_transaction, $payment_method , $request->input('bank_code'), false);

        if($payment_transaction && $payment_transaction->status == ApprovalStatus::APPROVED){
            $pL = $invoice_transaction->owner;
            $this->releaseGoodsToCustomerProcessor->execute($pL, $invoice_transaction);
        }

        //cief todo: at exchange there is a transition step - starts
        // if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::PAYMENT_GATEWAY){
        //     $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::PENDING_VERIFICATION);
        // }

        // if(config('perfexcrm.is_enabled') == 'true'){
        //     $this->transactionToPerfexCRMV2Processor->execute($transaction, $status);
        // }

        //To use
        //ApprovalStatus::PENDING_VERIFICATION;
        //use this to trigger  $this->transactionToPerfexCRMV2Processor->execute > defineTasks > defineTasks_handlePendingVerificationStatus > definePaymentTasks
        //cief todo: at exchange there is a transition step - ends

        return $this->resourceResponse(new TransactionResource($payment_transaction));
    }



}
