<?php

namespace App\Classes\Modules\Segments\ControllersLogic;

use App\Classes\Exceptions\ResourceNotFoundException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
use App\Classes\Modules\Segments\Services\UpdatesBasePriceConstantByDates;
use App\Classes\Modules\Segments\Services\CreatesConstant;
use App\Classes\Modules\Segments\Services\FetchesConstant;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\Modules\Segments\Standards\Rules\CanCreateConstant;
use App\Classes\Modules\Segments\Standards\Rules\CanUpdateConstant;
use App\Classes\Modules\Segments\Services\UpdatesConstant;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Http\Resources\ConstantResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateBasePriceLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Create Base Price',
            'message' => 'You have successfully created a Base Price'
        ];
    }

    /** @var CanUpdateConstant */
    private $canUpdateConstant;

    /** @var UpdatesConstant */
    private $updatesConstant;

    /** @var FetchesSegment */
    private $fetchesSegment;

    /** @var FetchesConstant */
    private $fetchesConstant;

    /** @var CanCreateConstant */
    private $canCreateConstant;

    /** @var CreatesConstant */
    private $createsConstant;


    /** @var UpdatesBasePriceConstantByDates */
    private $updatesBasePriceConstantByDates;

    /**
     * UpdateConstantLogic constructor.
     * @param CanUpdateConstant $canUpdateConstant
     * @param UpdatesConstant $updatesConstant
     * @param FetchesSegment $fetchesSegment
     * @param FetchesConstant $fetchesConstant
     * @param CanCreateConstant $canCreateConstant
     * @param CreatesConstant $createsConstant
     * @param UpdatesBasePriceConstantByDates $updatesBasePriceConstantByDates
     */
    public function __construct(
        CanUpdateConstant $canUpdateConstant, 
        UpdatesConstant $updatesConstant, 
        FetchesSegment $fetchesSegment, 
        FetchesConstant $fetchesConstant, 
        CanCreateConstant $canCreateConstant, 
        CreatesConstant $createsConstant,
        UpdatesBasePriceConstantByDates $updatesBasePriceConstantByDates
    )
    {
        $this->canUpdateConstant = $canUpdateConstant;
        $this->updatesConstant = $updatesConstant;
        $this->fetchesSegment = $fetchesSegment;
        $this->fetchesConstant = $fetchesConstant;
        $this->canCreateConstant = $canCreateConstant;
        $this->createsConstant = $createsConstant;
        $this->updatesBasePriceConstantByDates = $updatesBasePriceConstantByDates;
    }
    /**
     * @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
    {
        $segment = $this->fetchesSegment->execute(['id' => $request->route('id')]);

        try {

            $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => SegmentConstants::BASE_PRICE]);

        } catch (ResourceNotFoundException $exception){

            $constantObject = [];

            $object = new ConstantObject(
                SegmentConstants::BASE_PRICE,
                $constantObject
            );

            $constant = $this->createsConstant->execute($segment, $object);
        }

        $constantValue = $this->updatesBasePriceConstantByDates->execute($constant->value, $request->input('dateFrom'), $request->input('dateTo'), $request->input('rate'));

        $object = new ConstantObject(SegmentConstants::BASE_PRICE, $constantValue);

        $this->canUpdateConstant->passes($object);
        
        $constant = $this->updatesConstant->execute($constant, $object);

        return $this->resourceResponse(new ConstantResource($constant));
    }

}