<?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\DataTransferObjects\SegmentObject;
use App\Classes\Modules\Segments\Services\CreatesConstant;
use App\Classes\Modules\Segments\Services\CreatesSegment;
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\Http\Resources\ConstantResource;
use App\Models\SegmentConstant;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\Modules\Segments\Services\UpdatesConstantValue;

class UpdateConstantLogic  extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated Segment Constant',
            'message' => 'You have successfully updated the Segment Constant'
        ];
    }

    /** @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 CreatesSegment */
    private $createsSegment;

    /** @var UpdatesConstantValue */
    private $updatesConstantValue;

    /**
     * UpdateConstantLogic constructor.
     * @param CanUpdateConstant $canUpdateConstant
     * @param UpdatesConstant $updatesConstant
     * @param FetchesSegment $fetchesSegment
     * @param FetchesConstant $fetchesConstant
     * @param CanCreateConstant $canCreateConstant
     * @param CreatesConstant $createsConstant
     * @param UpdatesConstantValueByState $updatesConstantValueByState
     * @param CreatesSegment $createsSegment
     */
    public function __construct(
        CanUpdateConstant $canUpdateConstant, 
        UpdatesConstant $updatesConstant, 
        FetchesSegment $fetchesSegment, 
        FetchesConstant $fetchesConstant, 
        CanCreateConstant $canCreateConstant, 
        CreatesConstant $createsConstant,
        UpdatesConstantValue $updatesConstantValue,
        CreatesSegment $createsSegment
    )
    {
        $this->canUpdateConstant = $canUpdateConstant;
        $this->updatesConstant = $updatesConstant;
        $this->fetchesSegment = $fetchesSegment;
        $this->fetchesConstant = $fetchesConstant;
        $this->canCreateConstant = $canCreateConstant;
        $this->createsConstant = $createsConstant;
        $this->updatesConstantValue = $updatesConstantValue;
        $this->createsSegment = $createsSegment;
    }
    /**
     * @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' => $request->input('reference')]);

            $constantValue = $this->updatesConstantValue->execute($constant->value, $request->input('id'), $request->input('rate'));

            $object = new ConstantObject($request->input('reference'), $constantValue);

            $this->canUpdateConstant->passes($object);

        } catch (ResourceNotFoundException $exception){

            $constantObject = [];

            $constantObject[$request->input('id')] = $request->input('value');

            $object = new ConstantObject(
                $request->input('reference'),
                $constantObject
            );

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

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

}