<?php

namespace App\Classes\Modules\Companies\ControllersLogic;


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

class AssignCompanyConnectionToConnectionSegmentLogic extends AbstractControllerLogic
{

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

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

    /** @var FetchesCompanyConnection */
    private $fetchesCompanyConnection;

    /** @var AssignConnectionSegmentProcessor */
    private $assignCompanyConnectionToConnectionSegmentProcessor;

    /**
     * AssignCompanyToSegmentLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param FetchesCompanyConnection $fetchesCompanyConnection
     * @param AssignConnectionSegmentProcessor $assignCompanyConnectionToConnectionSegmentProcessor
     */
    public function __construct(FetchesCompany $fetchesCompany, FetchesCompanyConnection $fetchesCompanyConnection, AssignConnectionSegmentProcessor $assignCompanyConnectionToConnectionSegmentProcessor)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->fetchesCompanyConnection = $fetchesCompanyConnection;
        $this->assignCompanyConnectionToConnectionSegmentProcessor = $assignCompanyConnectionToConnectionSegmentProcessor;
    }

    /**
     * @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')]);

        $companyConnection = $this->fetchesCompanyConnection->execute(['id' => $request->route('company_connection_id')]);

        $this->assignCompanyConnectionToConnectionSegmentProcessor->execute($companyConnection, $request->input('segment_id'));

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