<?php

namespace App\Classes\Modules\Rules\ControllersLogic;


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

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

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


    /** @var CanPassMustEInvoiceAmountLimitRule */
    private $canPassMustEInvoiceAmountLimitRule;

    /**
     * CheckEInvoiceAmountLimitLogic constructor.
     * @param RuleEvaluator $ruleEvaluator
     * @param CanPassMustEInvoiceAmountLimitRule $canPassMustEInvoiceAmountLimitRule
     */
    public function __construct(RuleEvaluator $ruleEvaluator, CanPassMustEInvoiceAmountLimitRule $canPassMustEInvoiceAmountLimitRule)
    {
        $this->ruleEvaluator = $ruleEvaluator;
        $this->canPassMustEInvoiceAmountLimitRule = $canPassMustEInvoiceAmountLimitRule;
    }

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

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

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

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