<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Documents\Services\ApprovesDocument;
use App\Classes\Modules\Documents\Services\FetchesDocument;
use App\Classes\Modules\Documents\Services\RejectsDocument;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;

use App\Classes\Modules\Transactions\Processors\CreateCashBackTransactionProcessor;

use App\Classes\ValueObjects\Constants\ApprovalStatus;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ApprovePaymentVerificationLogic extends AbstractControllerLogic
{
    /**
     * ApprovePaymentVerificationLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param ApprovesDocument $approvesDocument
     * @param RejectsDocument $rejectsDocument
     * @param FetchesDocument $fetchesDocument
     * @param CreateCashBackTransactionProcessor $createCashBackTransactionProcessor
     */
    public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, ApprovesDocument $approvesDocument, RejectsDocument $rejectsDocument, FetchesDocument $fetchesDocument, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->approvesDocument = $approvesDocument;
        $this->rejectsDocument = $rejectsDocument;
        $this->createCashBackTransactionProcessor = $createCashBackTransactionProcessor;
    }

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Payment Status',
            'message' => 'You have successfully updated the payment status'
        ];
    }

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

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

    /** @var ApprovesDocument */
    private $approvesDocument;

    /** @var RejectsDocument */
    private $rejectsDocument;

    /** @var CreateCashBackTransactionProcessor */
    private $createCashBackTransactionProcessor;

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $status = $request->route('status');

        $transaction = $this->fetchesTransaction->execute(['id' => $request->route('payment_id')]);

        $status === 'approve' ? $this->approvesDocument->execute($transaction->documents()->first()) : $this->rejectsDocument->execute($transaction->documents()->first());

        $this->updatesTransactionStatus->execute($transaction, $status === 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED);

//        $this->createCashBackTransactionProcessor->execute($transaction);

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

}