<?php

namespace App\Jobs\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;
use Illuminate\Support\Facades\Log;

class ProcessHeartbeat 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 $machineService): void
    {
        try {
            $machineService->updateHeartbeat($this->serialNo, $this->data);
        } catch (\Exception $e) {
            Log::error("Failed to process heartbeat for machine {$this->serialNo}: " . $e->getMessage());
            throw $e;
        }
    }
}
