<?php

namespace App\Classes\Modules\Rules\ControllersLogic;


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

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

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

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

    /** @var CanPassEInvoicePromptedRule */
    private $canPassEInvoicePromptedRule;

    /** @var CanPassPurchaseOrderRule */
    private $canPassPurchaseOrderRule;


    /**
     * CheckPurchaseOrderRuleLogic constructor.
     * @param RuleEvaluator $ruleEvaluator
     * @param CanPassOrderDurationLimitRule $canPassOrderDurationLimitRule
     * @param CanPassEInvoicePromptedRule $canPassEInvoicePromptedRule
     * @param CanPassPurchaseOrderRule $canPassPurchaseOrderRule

     */
    public function __construct(RuleEvaluator $ruleEvaluator, CanPassOrderDurationLimitRule $canPassOrderDurationLimitRule, CanPassEInvoicePromptedRule $canPassEInvoicePromptedRule, CanPassPurchaseOrderRule $canPassPurchaseOrderRule)
    {
        $this->ruleEvaluator = $ruleEvaluator;
        $this->canPassOrderDurationLimitRule = $canPassOrderDurationLimitRule;
        $this->canPassEInvoicePromptedRule = $canPassEInvoicePromptedRule;
        $this->canPassPurchaseOrderRule = $canPassPurchaseOrderRule;
    }

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

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

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

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