<?php

namespace App\Jobs;

use App\Models\RoadObserved;
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 SoapClient;

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

    /**
     * The number of times the job may be attempted.
     *
     * @var int
     */
    public int $tries = 2;

    /**
     * Create a new job instance.
     */
    public function __construct(private RoadObserved $roadObserved){}

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try {
            $url = "https://riws.rmto.ir/WebServices/NRPTrafficStatus.asmx?wsdl";

            $array = array(
                'strUserName' => 'vaytelrop',
                'strPassword' => 'VaYtelROP*2024',
                'strAutoID' => $this->roadObserved->fk_RegisteredEventMessage,
                'strStateID' => '2',
                'strDescription' => 'گزارشات تایید شده از تاریخ 1 فروردین 1403 تا 14 مهر 1403',
                'strRMSDateAndTime' => $this->roadObserved->updated_at
            );

            $soapClient = new SoapClient($url);
            $result = $soapClient->UpdateInfo_RoadObservedProblems($array);
            Log::channel('road_observation_problem')->info(json_encode($result), [$this->roadObserved->id]);
        }
        catch (\Exception $exception){
            Log::channel('road_observation_problem')->error($exception->getMessage(), [
                'roadObserved' => $this->roadObserved->id,
                'line' => $exception->getLine(),
                'file' => $exception->getFile(),
            ]);
        }
    }
}
