<?php

namespace App\Classes\Modules\Banks\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Banks\Standards\Rules\CanCreateBank;
use App\Classes\Modules\Banks\Services\CreatesBank;
use App\Classes\Modules\Banks\DataTransferObjects\BankObject;
use App\Http\Resources\BankResource;

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

class CreateBankLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Bank',
            'message' => 'You have successfully created a new Bank'
        ];
    }

    /** @var CanCreateBank */
    private $canCreateBank;

    /** @var CreatesBank */
    private $createsBank;


    /**
     * CreateBankLogic constructor.
     * @param CanCreateBank $canCreateBank
     * @param CreatesBank $createsBank
     */
    public function __construct(
        CanCreateBank $canCreateBank,
        CreatesBank $createsBank
    )
    {
        $this->canCreateBank = $canCreateBank;
        $this->createsBank = $createsBank;
    }

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

        $bank_object = new BankObject($request->input('company_id'), $request->input('account_type'),
            $request->input('bank_name'), $request->input('holder_name'),  $request->input('account_no'),
             $request->input('bank_branch'),  $request->input('swift'),  $request->input('snap'),
            $request->input('country_id'), $request->input('reference'));

        $this->canCreateBank->passes($bank_object);

        $bank = $this->createsBank->execute($bank_object);

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

    }
}