<?php

namespace App\Classes\Modules\Accounts\ControllersLogic;

use App\Classes\Exceptions\AccessForbiddenException;
use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Accounts\Processors\AuthenticationProcessor;
use App\Classes\Modules\Accounts\Processors\CreateUserProcessor;
use App\Classes\Modules\Companies\Processors\AssignEmployeeProcessor;
use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Classes\Modules\Contacts\Processors\CreateContactProcessor;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
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 CreateInvitedCustomerLogic 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 CreateContactProcessor */
    private $createContactProcessor;

    /** @var FetchesCompany */
    private $fetchesCompany;

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

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


    /**
     * CreateInvitedCustomerLogic constructor.
     * @param CreateUserProcessor $createUserProcessor
     * @param CreateContactProcessor $createContactProcessor
     * @param FetchesCompany $fetchesCompany
     * @param AssignEmployeeProcessor $assignEmployeeProcessor
     * @param AuthenticationProcessor $authenticationProcessor
     */
    public function __construct(CreateUserProcessor $createUserProcessor, CreateContactProcessor $createContactProcessor, FetchesCompany $fetchesCompany, AssignEmployeeProcessor $assignEmployeeProcessor, AuthenticationProcessor $authenticationProcessor)
    {
        $this->createUserProcessor = $createUserProcessor;
        $this->createContactProcessor = $createContactProcessor;
        $this->fetchesCompany = $fetchesCompany;
        $this->assignEmployeeProcessor = $assignEmployeeProcessor;
        $this->authenticationProcessor = $authenticationProcessor;
    }

    /**
     * @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 Company $company */
        $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]);

        /** @var CompanyModule $companyModule */
        $companyModule = $company->companyModules()->first();

        if($companyModule->employees()->exists()){
            throw new AccessForbiddenException('This company\'s account has been claimed by another user already!');
        }

        /** @var User $user */
        $user = $this->createUserProcessor->execute($request, RoleTypes::USER, !App::environment(['production']) ? ApprovalStatus::APPROVED : ApprovalStatus::PENDING_VERIFICATION);

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

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

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

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

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