<?php

namespace App\Classes\Modules\Segments\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Segments\Standards\Rules\CanCreateSegment;
use App\Classes\Modules\Segments\Services\CreatesSegment;
use App\Classes\Modules\Segments\DataTransferObjects\SegmentObject;
use App\Http\Resources\SegmentResource;

use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CreateSegmentLogic extends AbstractControllerLogic
{

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

    /** @var CanCreateSegment */
    private $canCreateSegment;

    /** @var CreatesSegment */
    private $createsSegment;


    /**
     * CreateSegmentLogic constructor.
     * @param CanCreateSegment $canCreateSegment
     * @param CreatesSegment $createsSegment
     */
    public function __construct(CanCreateSegment $canCreateSegment, CreatesSegment $createsSegment)
    {
        $this->canCreateSegment = $canCreateSegment;
        $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_object = new SegmentObject($request->input('name'));

        $this->canCreateSegment->passes($segment_object);
        $segment = $this->createsSegment->execute($segment_object);

        return $this->resourceResponse(new SegmentResource($segment));

    }
}