<?php

namespace App\Classes\Modules\Companies\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Processors\AssignSegmentProcessor;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Http\Resources\CompanyResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AssignCompanyToSegmentLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Assign Company To Segment',
            'message' => 'You have successfully assigned Company to segment'
        ];
    }

    /** @var FetchesCompany */
    private $fetchesCompany;

    /** @var AssignSegmentProcessor */
    private $assignCompanyToSegmentProcessor;

    /**
     * AssignCompanyToSegmentLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param AssignSegmentProcessor $assignCompanyToSegmentProcessor
     */
    public function __construct(FetchesCompany $fetchesCompany, AssignSegmentProcessor $assignCompanyToSegmentProcessor)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->assignCompanyToSegmentProcessor = $assignCompanyToSegmentProcessor;
    }

    /**
     * @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
    {

        $company = $this->fetchesCompany->execute(['id' => $request->route('id')]);

        $this->assignCompanyToSegmentProcessor->execute($company, $request->input('segment_id'));

        return $this->resourceResponse(new CompanyResource($company));
    }
}