<?php

namespace App\Classes\Modules\Companies\Processors;

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

class AssignSegmentProcessor
{

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

    /** @var AssignsCompanyToSegment */
    private $assignsSegment;

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

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


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

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

        $this->canAssignSegment->passes();

        return $this->assignsSegment->execute($company, $segment);

    }

}