<?php

namespace App\Classes\Modules\Companies\Processors;

use App\Classes\General\Services\GeneratesInitials;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyObject;
use App\Classes\Modules\Companies\Services\CreatesCompany;
use App\Classes\Modules\Companies\Standards\Rules\CanCreateCompany;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\BusinessType;
use App\Classes\ValueObjects\Constants\CompanyType;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

class CreateCompanyProcessor
{

    /** @var CanCreateCompany */
    private $canCreateCompany;

    /** @var CreatesCompany */
    private $createsCompany;

    /**
     * CreateUserProcessor constructor.
     * @param CanCreateCompany $canCreateCompany
     * @param CreatesCompany $createsCompany
     */
    public function __construct(CanCreateCompany $canCreateCompany, CreatesCompany $createsCompany)
    {
        $this->canCreateCompany = $canCreateCompany;
        $this->createsCompany = $createsCompany;
    }


    /**
     * @param Request $request
     * @param int $businessType
     * @param int|null $companyType
     * @param int|null $status
     * @return Model
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(Request $request, int $businessType = BusinessType::IMPORTER, ?int $companyType = CompanyType::COMPANY_BUSINESS, ?int $status = ApprovalStatus::PENDING_SUBMISSION): Model {

        $companyName = $companyType === CompanyType::COMPANY_BUSINESS ? $request->input('company_name') : $request->input('name');
        $company_object = new CompanyObject(
            $companyName,
            mt_rand(1000, 9999).(new GeneratesInitials())->name($companyName)->length(3)->generate(),
            $businessType, $companyType, $status);

        $this->canCreateCompany->passes($company_object);

        return $this->createsCompany->execute($company_object);

    }

}