<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\Modules\Documents\Services\DeletesDocument;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

use App\Classes\Modules\Wallets\Processors\CreditWalletProcessor;


class UpdateRefundTransactionStatusLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated Transaction',
            'message' => 'You have successfully updated a transaction'
        ];
    }

    /** @var FetchesCompany */
    private $fetchesCompany;

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

    /** @var UpdatesTransactionStatus */
    private $updatesTransactionStatus;

    /** @var DeletesDocument */
    private $deletesDocument;

    /** @var CreditWalletProcessor */
    private $creditWalletProcessor;

    /**
     * CreatePaymentVerificationDocumentLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param DeletesDocument $deletesDocument
     * @param CreditWalletProcessor $creditWalletProcessor
     */
    public function __construct(FetchesCompany $fetchesCompany, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, DeletesDocument $deletesDocument, CreditWalletProcessor $creditWalletProcessor)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->deletesDocument = $deletesDocument;
        $this->creditWalletProcessor = $creditWalletProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]);

        $transaction = $this->updatesTransactionStatus->execute($transaction, $request->input('status'));

        $booking = $transaction->owner->owner;

        $reference = 'Credit Voucher for Overpaid for Ref. '.$booking->marking;

        if ($transaction->status == ApprovalStatus::APPROVED) {
            $this->creditWalletProcessor->execute($booking->company, $transaction->type, $transaction->amount, $reference);
        }



        return $this->response([]);
    }
}