<?php

namespace App\Classes\Modules\Accounts\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

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\Processors\AssignEmployeeProcessor;
use App\Classes\Modules\Companies\Processors\AssignSegmentProcessor;
use App\Classes\Modules\Companies\Processors\CreateCompanyProcessor;
use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject;
use App\Classes\Modules\Contacts\Processors\CreateContactProcessor;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\BusinessType;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Models\Company;
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 CreateContactProcessor */
    private $createContactProcessor;

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

    /** @var AssignSegmentProcessor */
    private $assignSegmentProcessor;

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

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

    /**
     * CreateCustomerLogic constructor.
     * @param CreateUserProcessor $createUserProcessor
     * @param CreateCompanyProcessor $createCompanyProcessor
     * @param CreateContactProcessor $createContactProcessor
     * @param AssignEmployeeProcessor $assignEmployeeProcessor
     * @param AssignSegmentProcessor $assignSegmentProcessor
     * @param AuthenticationProcessor $authenticationProcessor
     * @param GenerateEmailVerificationAttemptProcessor $generateEmailVerificationAttemptProcessor
     */
    public function __construct(CreateUserProcessor $createUserProcessor, CreateCompanyProcessor $createCompanyProcessor, CreateContactProcessor $createContactProcessor, AssignEmployeeProcessor $assignEmployeeProcessor, AssignSegmentProcessor $assignSegmentProcessor, AuthenticationProcessor $authenticationProcessor, GenerateEmailVerificationAttemptProcessor $generateEmailVerificationAttemptProcessor)
    {
        $this->createUserProcessor = $createUserProcessor;
        $this->createCompanyProcessor = $createCompanyProcessor;
        $this->createContactProcessor = $createContactProcessor;
        $this->assignEmployeeProcessor = $assignEmployeeProcessor;
        $this->assignSegmentProcessor = $assignSegmentProcessor;
        $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\InternalServerErrorException
     * @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(['local']) ? ApprovalStatus::APPROVED : ApprovalStatus::PENDING_VERIFICATION);

        /** @var Company $company */
        $company = $this->createCompanyProcessor->execute($request, BusinessType::IMPORTER, $request->input('type'), ApprovalStatus::PENDING_SUBMISSION);

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

        $Object = new EmploymentObject($company, $user);
        $this->assignEmployeeProcessor->execute($Object);

        $this->assignSegmentProcessor->execute($company);

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

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

    }
}