<?php

namespace App\Jobs\Machine;

use App\Models\Machine\Machine;
use App\Services\Machine\MachineService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    protected $serialNo;
    protected $data;

    /**
     * Create a new job instance.
     */
    public function __construct(string $serialNo, array $data)
    {
        $this->serialNo = $serialNo;
        $this->data = $data;
    }

    /**
     * Execute the job.
     */
    public function handle(MachineService $service): void
    {
        $machine = Machine::where('serial_no', $this->serialNo)->first();

        if ($machine) {
            $service->recordErrorLog($machine, $this->data);
        }
    }
}
