<?php

namespace App\Classes\Modules\Companies\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\RemovesCompanyFromSegment;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Http\Resources\CompanyResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RemoveCompanyFromSegmentLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Detach Company From Segment',
            'message' => 'You have successfully detached Company from segment'
        ];
    }

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

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

    /** @var RemovesCompanyFromSegment */
    private $removesCompanyFromSegment;

    /**
     * RemoveCompanyFromSegmentLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param FetchesSegment $fetchesSegment
     * @param RemovesCompanyFromSegment $removesCompanyFromSegment
     */
    public function __construct(FetchesCompany $fetchesCompany, FetchesSegment $fetchesSegment, RemovesCompanyFromSegment $removesCompanyFromSegment)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->fetchesSegment = $fetchesSegment;
        $this->removesCompanyFromSegment = $removesCompanyFromSegment;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {

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

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

        $this->removesCompanyFromSegment->execute($company, $segment);

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