<?php

namespace App\Classes\Modules\Companies\Processors;

use App\Classes\Modules\Companies\Services\AssignsCompanyConnectionToConnectionSegment;
use App\Classes\Modules\Companies\Standards\Rules\CanAssignSegment;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Models\CompanyConnection;
use Illuminate\Database\Eloquent\Model;

class AssignConnectionSegmentProcessor
{

    /** @var CanAssignSegment */
    private $canAssignSegment;

    /** @var AssignsCompanyConnectionToConnectionSegment */
    private $assignsCompanyConnectionToConnectionSegment;

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

    /**
     * AssignCompanyToSegmentProcessor constructor.
     * @param CanAssignSegment $canAssignSegment
     * @param AssignsCompanyConnectionToConnectionSegment $assignsCompanyConnectionToConnectionSegment
     * @param FetchesSegment $fetchesSegment
     */
    public function __construct(CanAssignSegment $canAssignSegment, AssignsCompanyConnectionToConnectionSegment $assignsCompanyConnectionToConnectionSegment, FetchesSegment $fetchesSegment)
    {
        $this->canAssignSegment = $canAssignSegment;
        $this->assignsCompanyConnectionToConnectionSegment = $assignsCompanyConnectionToConnectionSegment;
        $this->fetchesSegment = $fetchesSegment;
    }


    /**
     * @param CompanyConnection $companyConnection
     * @param int|null $segmentId
     * @return Model
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(CompanyConnection $companyConnection, ?int $segmentId = SegmentConstants::STANDARD_SEGMENT): Model {

        $segment = $this->fetchesSegment->execute(['id' => $segmentId]);

        $this->canAssignSegment->passes();

        return $this->assignsCompanyConnectionToConnectionSegment->execute($companyConnection, $segment);

    }

}