<?php

namespace App\Classes\Modules\Rules\ControllersLogic;


use App\Classes\Exceptions\CriteriaNotFulfilledException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Rules\DataTransferObjects\CheckTransferPeriodLockRuleDTO;
use App\Classes\Modules\Rules\Services\RuleEvaluator;
use App\Classes\Modules\Rules\Standards\Rules\CanPassTransferPeriodLockRule;
use App\Http\Resources\RuleResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CheckTransferPeriodLockRuleLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Rule Check Transfer Period Lock',
            'message' => 'You have successfully passed all rules evaluated'
        ];
    }

    /** @var RuleEvaluator */
    private $ruleEvaluator;

    /** @var CanPassTransferPeriodLockRule */
    private $canPassTransferPeriodLockRule;

    /**
     * CheckTransferPeriodLockRuleLogic constructor.
     * @param RuleEvaluator $ruleEvaluator
     * @param CanPassTransferPeriodLockRule $canPassTransferPeriodLockRule
     */
    public function __construct(RuleEvaluator $ruleEvaluator, CanPassTransferPeriodLockRule $canPassTransferPeriodLockRule)
    {
        $this->ruleEvaluator = $ruleEvaluator;
        $this->canPassTransferPeriodLockRule = $canPassTransferPeriodLockRule;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     * @throws \App\Classes\Exceptions\CriteriaNotFulfilledException
     */
    public function logic(Request $request) : JsonResponse
    {
        $dto = new CheckTransferPeriodLockRuleDTO($request->all());

        $result = $this->ruleEvaluator->evaluate([
            $this->canPassTransferPeriodLockRule,
        ], $dto);

        if ($result->failed()) {
            throw new CriteriaNotFulfilledException("- " . implode("<br>- ", $result->messages()));
        }

        return $this->resourceResponse(new RuleResource((object)$result));
    }
}
