<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\ListsTransactions;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Http\Resources\BookingResource;
use App\Http\Resources\TransactionResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateTransactionStatusLogic extends AbstractControllerLogic
{

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

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

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


    /**
     * SuspendTransactionLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     */
    public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
    }


    public function logic(Request $request) : JsonResponse
    {
        $approvalArray = [
            'approve' => ApprovalStatus::APPROVED,
            'expire' => ApprovalStatus::EXPIRED,
            'reject' => ApprovalStatus::REJECTED,
        ];

        $approvalStatus = $approvalArray[$request->route('status')];

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

        if ($request->route('status') === 'reject') {
            $transaction->delete();
        } else {
            $transaction = $this->updatesTransactionStatus->execute($transaction, $approvalStatus);
        }

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



}
