<?php

namespace App\Jobs;

use App\Services\LogisticYDService;
use App\Services\LogisticYTOService;
use App\Services\LogService;
use App\Services\OrderPackageReceivedSyncService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class LogisticYTOSync implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * @var $logisticYTOService LogisticYTOService
     * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
     * @var $logistic_number string
     */

    protected $logistic_number;

    protected $logisticYTOService;
    protected $orderPackageReceivedSyncService;

    /**
     * LogisticYDSync constructor.
     * @param $logistic_number
     */
    public function __construct($logistic_number)
    {
        $this->logistic_number = $logistic_number;
    }


    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        ini_set('max_execution_time', 60);
        LogService::log(LogisticYTOSync::class, "{$this->logistic_number}-JOB-YTO", '');
        $this->logisticYTOService = app('LogisticYTOService');
        $nativeResponse = $this->logisticYTOService->query($this->logistic_number);
        $formattedData = $this->logisticYTOService->format($nativeResponse);
        $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
        $this->orderPackageReceivedSyncService->update([$formattedData]);
    }
}
