<?php

namespace App\Classes\Modules\Companies\Processors;

use App\Classes\General\Services\GeneratesInitials;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyConnectionObject;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyObject;
use App\Classes\Modules\Companies\Services\CreatesCompany;
use App\Classes\Modules\Companies\Services\CreatesCompanyConnection;
use App\Classes\Modules\Companies\Services\GeneratesUniqueAccountNumber;
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;

    /** @var GeneratesUniqueAccountNumber */
    private $generatesUniqueAccountNumber;

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


    /**
     * @param string $name
     * @param int $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(string $name, int $companyType = CompanyType::COMPANY_BUSINESS, ?int $status = ApprovalStatus::PENDING_SUBMISSION): Model {

        $company_object = new CompanyObject($name, $this->generatesUniqueAccountNumber->execute(), $companyType, $status);

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

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

    }

}
