<?php

namespace App\Classes\Modules\Banks\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Banks\Standards\Rules\CanUpdateBankMetadata;
use App\Classes\Modules\Banks\DataTransferObjects\BankMetadataObject;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateBankMetadataLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdateBankMetadata */
    private $canUpdateBankMetadata;

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

    /**
     * UpdateBankMetadataLogic constructor.
     * @param CanUpdateBankMetadata $canUpdateBankMetadata
     * @param FetchesTransaction $fetchesTransaction
     */
    public function __construct(
        CanUpdateBankMetadata $canUpdateBankMetadata,
        FetchesTransaction $fetchesTransaction
    )
    {
        $this->canUpdateBankMetadata = $canUpdateBankMetadata;
        $this->fetchesTransaction = $fetchesTransaction;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {
        $bankMetadataObject = new BankMetadataObject(
            $request->input('transactionId'),
        );

        $this->canUpdateBankMetadata->passes($bankMetadataObject);

        $transaction = $this->fetchesTransaction->execute(['id' => $request->input('transactionId')]);
        $transaction->attributesKVP()->delete();

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

}
