<?php

namespace App\Classes\Modules\SegmentConstants\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Segments\Standards\Rules\CanCreateConstant;
use App\Classes\Modules\Segments\Services\CreatesConstant;
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Http\Resources\ConstantResource;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateSegmentConstantLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Segment Constant',
            'message' => 'You have successfully created a new Segment Constant'
        ];
    }

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

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

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


    /**
     * CreateSegmentLogic constructor.
     * @param CanCreateConstant $canCreateConstant
     * @param CreatesConstant $createsConstant
     */
    public function __construct(CanCreateConstant $canCreateConstant, CreatesConstant $createsConstant, FetchesSegment $fetchesSegment)
    {
        $this->canCreateConstant = $canCreateConstant;
        $this->createsConstant = $createsConstant;
        $this->fetchesSegment = $fetchesSegment;
    }

    /**
     * @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
    {
        $constant_object = new ConstantObject($request->input('name'), $request->input('reference'), $request->input('detail'));

        $segment = $this->fetchesSegment->execute(['id' => $request->input('segment_id')]);

        $this->canCreateConstant->passes($constant_object);
        $constant = $this->createsConstant->execute($segment, $constant_object);

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

    }
}