<?php

namespace App\Classes\Modules\PerfexCRM\Processors;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\Modules\PerfexCRM\DataTransferObjects\UpdatePerfexCRMObject;
use App\Classes\Jobs\UpdatePerfexCRM;
use App\Models\Booking;
use App\Classes\ValueObjects\Constants\PerfexCRMProjectStatus;

class BookingToPerfexCRMProcessor
{
    /**
     * @param Booking $booking
     * @return bool
     * @throws MalformedRequestException
     */
    public function execute(Booking $booking): bool
    {
        $companyReference = $booking->company->reference;
        $companyName = $booking->company->name;
        $employee = $booking->company->employees()->first();
        $contactEmail = $employee->email;
        $contactName = $employee->name;
        $bookingMarking = $booking->marking;

        $serviceTypeName = $booking->company->services()->where('id', $booking->service_id)->first()->name;
        $projectName = 'Exchange | '.$serviceTypeName.' | '.$bookingMarking;

        $updatePerfexCRMObject = new UpdatePerfexCRMObject(
            $companyName,
            $companyReference,
            $contactName,
            $contactEmail,
            $bookingMarking,
            $projectName,
            PerfexCRMProjectStatus::NOT_STARTED,
            0,
            [],
            []
        );

        UpdatePerfexCRM::dispatch($updatePerfexCRMObject, null, null);

        return true;
    }
}
