<?php

namespace App\Jobs;

use App\DeliveryAppointment;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class DeliveryAppointmentCheck implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable;


    protected $int;
    /**
     * Create a new job instance.
     *
     * @param int $int
     *
     * @return void
     */
    public function __construct(int $int)
    {
        $this->int = $int;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        /** @var DeliveryAppointment|\stdClass $appointment */
        $appointment = DeliveryAppointment::query()->find($this->int);
        $time = explode("-",DeliveryAppointment::PERIOD[$appointment->date_period])[1];
        if ($appointment && $appointment->status==0
            && Carbon::now()->gt(Carbon::parse($appointment->appointment_date." ".$time.":00:00"))
        )$appointment->update(["status"=>3]);
    }
}
