<?php

namespace App\Classes\Modules\Companies\Processors;

use App\Classes\Modules\Unity\Processors\CreateEntityProcessor;
use App\Classes\Modules\Unity\Processors\CreateEntitySignatureProcessor;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyModuleObject;
use App\Classes\Modules\Companies\Services\CreatesCompanyModule;
use App\Classes\Modules\Companies\Standards\Rules\CanCreateCompanyModule;
use App\Classes\ValueObjects\Constants\BusinessType;
use App\Models\CompanyModule;
use Illuminate\Database\Eloquent\Model;
use App\Models\Company;

class CreateCompanyModuleProcessor
{

    /** @var CreatesCompanyModule */
    private $createsCompanyModule;

    /** @var CreateEntityProcessor  */
    private $createUnityEntityProcessor;

    /** @var CanCreateCompanyModule  */
    private $canCreateCompanyModule;

    /** @var CreateEntityProcessor*/
    private $createsEntityProcessor;

    /** @var CreateEntitySignatureProcessor  */
    private $createUnityEntitySignatureProcessor;

    /**
     * CreateCompanyModuleProcessor constructor.
     * @param CreatesCompanyModule $createsCompanyModule
     * @param CreateEntityProcessor $createUnityEntityProcessor
     * @param CanCreateCompanyModule $canCreateCompanyModule
     * @param CreateEntityProcessor $createsEntityProcessor
     * @param CreateEntitySignatureProcessor $createUnityEntitySignatureProcessor
     */
    public function __construct(CreatesCompanyModule $createsCompanyModule, CreateEntityProcessor $createUnityEntityProcessor, CanCreateCompanyModule $canCreateCompanyModule, CreateEntityProcessor $createsEntityProcessor, CreateEntitySignatureProcessor $createUnityEntitySignatureProcessor)
    {
        $this->createsCompanyModule = $createsCompanyModule;
        $this->createUnityEntityProcessor = $createUnityEntityProcessor;
        $this->canCreateCompanyModule = $canCreateCompanyModule;
        $this->createsEntityProcessor = $createsEntityProcessor;
        $this->createUnityEntitySignatureProcessor = $createUnityEntitySignatureProcessor;
    }


    /**
     * @param Company $company
     * @param int|null $businessType
     * @return Model
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(Company $company , ?int $businessType = BusinessType::IMPORTER): Model
    {
        $unityEntity = $this->createUnityEntityProcessor->execute($company->name);

        $unityEntitySignature = $this->createUnityEntitySignatureProcessor->execute($unityEntity->hash_id);

        $company_module_object = new CompanyModuleObject($company->name, $company->reference, $unityEntity->hash_id, $unityEntitySignature->hash_id, $businessType, $company->status);

        $this->canCreateCompanyModule->passes($company_module_object);

        /** @var CompanyModule $companyModule */
        $companyModule = $this->createsCompanyModule->execute($company, $company_module_object);

        return $companyModule;
    }
}
