<?php namespace App\Jobs\HubSpot;

use App\Models\Order;
use App\Services\Navision\ContactsService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Throwable;

class UpdateCustomer implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * The webhook data
     *
     * @var Array
     */
    public $hubspotData;

    /**
     * Create a new job instance.
     *
     * @param Order $data - The order to be integrated with NAV.
     *
     * @return void
     */
    public function __construct(Array $hubspotData)
    {
        $this->hubspotData = $hubspotData;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(ContactsService $service)
    {
        Log::info('HubSpot Customer Update - updating... (hubspot object id: ' . $this->hubspotData['objectId'] . ')');
        $service->updateCustomerFromHubspot($this->hubspotData['objectId']);
    }

    /**
     * Handle a job failure.
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function failed(Throwable $exception)
    {
        Log::error('HubSpot Customer Update - Job Error: ' . $exception->getMessage());
    }
}
