<?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\Classes\Modules\Banks\Services\CreatesBankLog;
use App\Classes\ValueObjects\Constants\RoleTypes;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

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;

    /** @var CreatesBankLog */
    private $createsBankLog;

    /**
     * DeleteBankLogic constructor.
     * @param CanDeleteBank $canDeleteBank
     * @param DeletesBank $deletesBank
     * @param FetchesBank $fetchesBank
     * @param CreatesBankLog $createsBankLog
     */
    public function __construct(
        CanDeleteBank $canDeleteBank,
        DeletesBank $deletesBank,
        FetchesBank $fetchesBank,
        CreatesBankLog $createsBankLog
    )
    {
        $this->canDeleteBank = $canDeleteBank;
        $this->deletesBank = $deletesBank;
        $this->fetchesBank = $fetchesBank;
        $this->createsBankLog = $createsBankLog;
    }

    /**
     * @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')]);

        $proceed = false;
        if($bank->default){
            $isAuthorized = in_array(Auth::user()->type, RoleTypes::ADMIN_ROLES);
            if($isAuthorized) {
                $banks = $bank->company->banks()->where('default', 1)->get();
                if(count($banks) > 1) {
                    //User should be able to delete themselves, but sometimes there are more than 1 bank set as default (different type, why??!), we need to allow admin to do the delete
                    $proceed = true;
                }
                else{
                    $proceed = false;
                }
            }

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

        $bank = $this->deletesBank->execute($bank);
        // $bankLog = $this->createsBankLog->execute($bank);
        return $this->response([]);
    }
}
