<?php

namespace App\Classes\Modules\Rules\ControllersLogic;


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

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

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

    /** @var CanPassOrderDurationLimitRule */
    private $canPassOrderDurationLimitRule;

    /**
     * CheckTransferRuleLogic constructor.
     * @param RuleEvaluator $ruleEvaluator
     * @param CanPassOrderDurationLimitRule $canPassOrderDurationLimitRule
     */
    public function __construct(RuleEvaluator $ruleEvaluator, CanPassOrderDurationLimitRule $canPassOrderDurationLimitRule)
    {
        $this->ruleEvaluator = $ruleEvaluator;
        $this->canPassOrderDurationLimitRule = $canPassOrderDurationLimitRule;
    }

    /**
     * @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 CheckTransferRuleDTO($request->all());

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

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

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