<?php

namespace App\Classes\Jobs;

use App\Classes\Modules\PerfexCRM\Processors\CreatePerfexCRMTaskProcessor;
use App\Classes\Modules\PerfexCRM\Services\FetchesPerfexCRMLead;
use App\Classes\Modules\PerfexCRM\DataTransferObjects\CreateTaskPerfexCRMObject;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CreatePerfexCRMSingleTask implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /** @var FetchesPerfexCRMLead  */
    private $fetchesPerfexCRMLead;

    /** @var CreateTaskPerfexCRMObject */
    private $createTaskPerfexCRMObject;

    /**
     * CreatePerfexCRMSingleTask constructor.
     * @param CreateTaskPerfexCRMObject $createTaskPerfexCRMObject
     */
    public function __construct(CreateTaskPerfexCRMObject $createTaskPerfexCRMObject)
    {
        $this->createTaskPerfexCRMObject = $createTaskPerfexCRMObject;
    }

    public function handle()
    {
        $lead = (App()->make(FetchesPerfexCRMLead::class))->execute($this->createTaskPerfexCRMObject->getEmail());
        if(!is_null($lead))
        {
            $this->createTaskPerfexCRMObject->setLeadId($lead->id);
            (App()->make(CreatePerfexCRMTaskProcessor::class))->execute($this->createTaskPerfexCRMObject);
        }
    }
}
