<?php

namespace App\Classes\Modules\Banks\ControllersLogic;

use App\Http\Resources\BankResource;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Banks\Services\FetchesBank;

use App\Classes\Modules\Banks\Standards\Rules\CanUpdateBank;
use App\Classes\Modules\Banks\Services\UpdatesBank;
use App\Classes\Modules\Banks\DataTransferObjects\BankObject;

use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UpdateBankLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdateBank */
    private $canUpdateBank;

    /** @var UpdatesBank */
    private $updatesBank;

    /** @var FetchesBank */
    private $fetchesBank;


    /**
     * UpdateBankLogic constructor.
     * @param CanUpdateBank $canUpdateBank
     * @param UpdatesBank $updatesBank
     * @param FetchesBank $fetchesBank
     */
    public function __construct(
        CanUpdateBank $canUpdateBank,
        UpdatesBank $updatesBank,
        FetchesBank $fetchesBank
    )
    {
        $this->canUpdateBank = $canUpdateBank;
        $this->updatesBank = $updatesBank;
        $this->fetchesBank = $fetchesBank;
    }

    /**
     * @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
    {

        $bankObject = new BankObject($request->input('company_id'), $request->input('country_id'),
            $request->input('reference'), $request->input('bank_name'), $request->input('holder_name'),
            $request->input('account_no'));


        $bank = $this->fetchesBank->execute(['id' => $request->route('id')]);

        $this->canUpdateBank->passes($bankObject);

        $bank_query = $this->updatesBank->execute($bank, $bankObject);

        return $this->resourceResponse(new BankResource($bank_query));

    }

}