<?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\Contacts\Processors\CreateContactProcessor;
use App\Classes\Modules\Milestones\Processors\CheckMilestonesForRewardProcessor;
use App\Classes\Modules\Vouchers\Processors\Voucherify\NewCustomerToVoucherifyProcessor;
use App\Classes\Modules\Vouchers\Processors\CreateVoucherProcessor;
use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject;
use App\Classes\Modules\PerfexCRM\DataTransferObjects\CreateLeadPerfexCRMObject;
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\Classes\Jobs\CreatePerfexCRMCustomer;
use App\Classes\Modules\Segments\DataTransferObjects\SeasonalSegmentObject;
use App\Models\Company;
use App\Models\Segment;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use App\Classes\Modules\Segments\Services\CreatesSeasonalSegment;

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;

    /** @var CreatesSeasonalSegment */
    private $createsSeasonalSegment;

    /** @var CheckMilestonesForRewardProcessor */
    private $checkMilestonesForRewardProcessor;

    /** @var NewCustomerToVoucherifyProcessor */
    private $newCustomerToVoucherifyProcessor;

    /** @var CreateVoucherProcessor */
    private $createVoucherProcessor;

    /**
     * CreateCustomerLogic constructor.
     * @param CreateUserProcessor $createUserProcessor
     * @param CreateCompanyProcessor $createCompanyProcessor
     * @param CreateContactProcessor $createContactProcessor
     * @param AssignEmployeeProcessor $assignEmployeeProcessor
     * @param AssignSegmentProcessor $assignSegmentProcessor
     * @param AuthenticationProcessor $authenticationProcessor
     * @param GenerateEmailVerificationAttemptProcessor $generateEmailVerificationAttemptProcessor
     * @param CreatesSeasonalSegment $createsSeasonalSegment
     * @param CheckMilestonesForRewardProcessor $checkMilestonesForRewardProcessor
     * @param NewCustomerToVoucherifyProcessor $newCustomerToVoucherifyProcessor
     * @param CreateVoucherProcessor $createVoucherProcessor
     */
    public function __construct(CreateUserProcessor $createUserProcessor, CreateCompanyProcessor $createCompanyProcessor, CreateContactProcessor $createContactProcessor, AssignEmployeeProcessor $assignEmployeeProcessor, AssignSegmentProcessor $assignSegmentProcessor, AuthenticationProcessor $authenticationProcessor, GenerateEmailVerificationAttemptProcessor $generateEmailVerificationAttemptProcessor,
    CreatesSeasonalSegment $createsSeasonalSegment, CheckMilestonesForRewardProcessor $checkMilestonesForRewardProcessor, NewCustomerToVoucherifyProcessor $newCustomerToVoucherifyProcessor, CreateVoucherProcessor $createVoucherProcessor)
    {
        $this->createUserProcessor = $createUserProcessor;
        $this->createCompanyProcessor = $createCompanyProcessor;
        $this->createContactProcessor = $createContactProcessor;
        $this->assignEmployeeProcessor = $assignEmployeeProcessor;
        $this->assignSegmentProcessor = $assignSegmentProcessor;
        $this->authenticationProcessor = $authenticationProcessor;
        $this->generateEmailVerificationAttemptProcessor = $generateEmailVerificationAttemptProcessor;
        $this->createsSeasonalSegment = $createsSeasonalSegment;
        $this->checkMilestonesForRewardProcessor = $checkMilestonesForRewardProcessor;
        $this->newCustomerToVoucherifyProcessor = $newCustomerToVoucherifyProcessor;
        $this->createVoucherProcessor = $createVoucherProcessor;
    }

    /**
     * @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') === CompanyType::COMPANY_BUSINESS ? CompanyType::COMPANY_BUSINESS : CompanyType::PERSONAL_BUSINESS, ApprovalStatus::PENDING_SUBMISSION);

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

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

        // assign STANDARD_SEGMENT to all new customers
        $this->assignSegmentProcessor->execute($company);

        // assign other standard segment to all new customers
        $otherDefaultSegmentToAdd = Segment::whereIn('name', ['HONEY TRAP NEW REGISTRATION'])->get();
        $start_date     = Carbon::now();
        $end_date       = Carbon::now()->addDays(30);
        foreach ($otherDefaultSegmentToAdd as $segment) {
            $seasonalSegmentObject = new SeasonalSegmentObject($company->id, $segment->id, $start_date, $end_date ?? null);

            $this->createsSeasonalSegment->execute($seasonalSegmentObject);
            $this->assignSegmentProcessor->execute($company, $segment->id);
        }

        if(config('perfexcrm.is_enabled') == 'true'){
            $createLeadPerfexCRMObject = new CreateLeadPerfexCRMObject(
                $request->input('name'),
                $request->input('email'),
                $request->input('phone'),
                $request->input('type') === CompanyType::COMPANY_BUSINESS ? $request->input('company_name') : $request->input('name'),
                $company->reference
            );
            CreatePerfexCRMCustomer::dispatch($createLeadPerfexCRMObject);
        }

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

        $this->newCustomerToVoucherifyProcessor->execute($company->id, $user, true);

        $this->createVoucherProcessor->execute($user, 'WELCOME50%OFF');

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

    }
}
