<?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\Services\CreatesBankLog;
use App\Classes\Modules\Banks\DataTransferObjects\BankObject;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Milestones\Processors\CheckMilestonesForRewardProcessor;
use App\Http\Resources\BankResource;
use App\Classes\ValueObjects\Constants\Milestones;

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;

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

    /** @var FetchesCompany */
    private $fetchesCompany;

    /** @var CheckMilestonesForRewardProcessor */
    private $checkMilestonesForRewardProcessor;


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

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

//        $bankLog = $this->createsBankLog->execute($bank);

        //cief todo: case study 4 voucherify
        // $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]);
        // $user = $company->employees()->first();
        // $this->checkMilestonesForRewardProcessor->execute($user, [Milestones::MILESTONE_4]);

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

    }
}
