<?php

namespace App\Classes\Modules\Banks\ControllersLogic;

use App\Classes\Exceptions\RequestValidationException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Banks\Services\FetchesBank;
use App\Classes\Modules\Banks\Standards\Rules\CanDeleteBank;
use App\Classes\Modules\Banks\Services\DeletesBank;
use App\Http\Resources\BankResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DeleteBankLogic extends AbstractControllerLogic
{

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

    /** @var CanDeleteBank */
    private $canDeleteBank;

    /** @var DeletesBank */
    private $deletesBank;

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


    /**
     * DeleteBankLogic constructor.
     * @param CanDeleteBank $canDeleteBank
     * @param DeletesBank $deletesBank
     * @param FetchesBank $fetchesBank
     */
    public function __construct(
        CanDeleteBank $canDeleteBank,
        DeletesBank $deletesBank,
        FetchesBank $fetchesBank
    )
    {
        $this->canDeleteBank = $canDeleteBank;
        $this->deletesBank = $deletesBank;
        $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
    {

        $this->canDeleteBank->passes();

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

        if($bank->default){
            throw new RequestValidationException('You can\'t delete bank account when it set to default');
        }

        $this->deletesBank->execute($bank);

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

}