<?php

namespace App\Classes\Modules\Accounts\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\General\Services\GeneratesInitials;
use App\Classes\Modules\Accounts\Processors\AuthenticationProcessor;
use App\Classes\Modules\Accounts\Processors\CreateUserProcessor;
use App\Classes\Modules\Accounts\Processors\GenerateEmailVerificationAttemptProcessor;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyConnectionObject;
use App\Classes\Modules\Companies\Processors\AssignEmployeeProcessor;
use App\Classes\Modules\Companies\Processors\CreateCompanyProcessor;
use App\Classes\Modules\Companies\Processors\CreateCompanyModuleProcessor;
use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject;
use App\Classes\Modules\Companies\Services\ApprovesCompanyConnection;
use App\Classes\Modules\Companies\Services\CreatesCompanyConnection;
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Classes\Modules\Contacts\Processors\CreateContactProcessor;
use App\Classes\Modules\Documents\Processors\UploadIdentityDocumentProcessor;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\BusinessType;
use App\Classes\ValueObjects\Constants\CompanyType;
use App\Classes\ValueObjects\Constants\RoleTypes;

use App\Models\Company;
use App\Models\CompanyModule;
use App\Models\User;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class CreateCustomerLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Created User',
            'message' => 'You have successfully created a new User'
        ];
    }

    /** @var CreateUserProcessor */
    private $createUserProcessor;

    /** @var CreateCompanyProcessor */
    private $createCompanyProcessor;

    /** @var CreateCompanyModuleProcessor */
    private $createCompanyModuleProcessor;

    /** @var CreateContactProcessor */
    private $createContactProcessor;

    /** @var CreatesCompanyConnection */
    private $createsCompanyConnection;

    /** @var ApprovesCompanyConnection */
    private $approvesCompanyConnection;

    /** @var AssignEmployeeProcessor */
    private $assignEmployeeProcessor;

    /** @var UploadIdentityDocumentProcessor */
    private $uploadIdentityDocumentProcessor;

    /** @var AuthenticationProcessor */
    private $authenticationProcessor;

    /** @var GenerateEmailVerificationAttemptProcessor */
    private $generateEmailVerificationAttemptProcessor;

    /**
     * CreateCustomerLogic constructor.
     * @param CreateUserProcessor $createUserProcessor
     * @param CreateCompanyProcessor $createCompanyProcessor
     * @param CreateCompanyModuleProcessor $createCompanyModuleProcessor
     * @param CreateContactProcessor $createContactProcessor
     * @param CreatesCompanyConnection $createsCompanyConnection
     * @param ApprovesCompanyConnection $approvesCompanyConnection
     * @param AssignEmployeeProcessor $assignEmployeeProcessor
     * @param UploadIdentityDocumentProcessor $uploadIdentityDocumentProcessor
     * @param AuthenticationProcessor $authenticationProcessor
     * @param GenerateEmailVerificationAttemptProcessor $generateEmailVerificationAttemptProcessor
     */
    public function __construct(CreateUserProcessor $createUserProcessor, CreateCompanyProcessor $createCompanyProcessor, CreateCompanyModuleProcessor $createCompanyModuleProcessor, CreateContactProcessor $createContactProcessor, CreatesCompanyConnection $createsCompanyConnection, ApprovesCompanyConnection $approvesCompanyConnection, AssignEmployeeProcessor $assignEmployeeProcessor, UploadIdentityDocumentProcessor $uploadIdentityDocumentProcessor, AuthenticationProcessor $authenticationProcessor, GenerateEmailVerificationAttemptProcessor $generateEmailVerificationAttemptProcessor)
    {
        $this->createUserProcessor = $createUserProcessor;
        $this->createCompanyProcessor = $createCompanyProcessor;
        $this->createCompanyModuleProcessor = $createCompanyModuleProcessor;
        $this->createContactProcessor = $createContactProcessor;
        $this->createsCompanyConnection = $createsCompanyConnection;
        $this->approvesCompanyConnection = $approvesCompanyConnection;
        $this->assignEmployeeProcessor = $assignEmployeeProcessor;
        $this->uploadIdentityDocumentProcessor = $uploadIdentityDocumentProcessor;
        $this->authenticationProcessor = $authenticationProcessor;
        $this->generateEmailVerificationAttemptProcessor = $generateEmailVerificationAttemptProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\AccessUnauthorisedException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request): JsonResponse
    {
        /** @var User $user */
        $user = $this->createUserProcessor->execute($request, RoleTypes::USER, !App::environment(['production']) ? ApprovalStatus::APPROVED : ApprovalStatus::PENDING_VERIFICATION);

        /** @var Company $company */
        $company = $this->createCompanyProcessor->execute($request->input('type') === CompanyType::COMPANY_BUSINESS ? $request->input('company_name') : $request->input('name'), $request->input('type'), ApprovalStatus::PENDING_SUBMISSION);

        $contactObject = new ContactObject($request->input('name'), $request->input('phone'), $request->input('contact_email'), $request->input('wechat_id'));

        $this->createContactProcessor->execute($contactObject, $company);

        /** @var CompanyModule $companyModule */
        $companyModule = $this->createCompanyModuleProcessor->execute($company, BusinessType::IMPORTER);

        $connectionObject = new CompanyConnectionObject($companyModule, 'CIEF', mt_rand(1000, 9999).(new GeneratesInitials())->name($company->name)->length(3)->generate());

        $connection = $this->createsCompanyConnection->execute($connectionObject);

        $this->approvesCompanyConnection->execute($connection);

        $Object = new EmploymentObject($companyModule, $user);

        $this->assignEmployeeProcessor->execute($Object);

        $this->uploadIdentityDocumentProcessor->execute($request, $company);

//        $this->generateEmailVerificationAttemptProcessor->execute($user);

        return $this->response($this->authenticationProcessor->execute($request));
    }
}
