<?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 ProcessMachineLog implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * @var int
     */
    protected $machineId;

    /**
     * @var array
     */
    protected $logData;

    public function __construct(int $machineId, array $logData)
    {
        $this->machineId = $machineId;
        $this->logData = $logData;
    }

    public function getMachineId(): int
    {
        return $this->machineId;
    }

    public function handle(MachineService $service): void
    {
        try {
            $service->recordLog($this->machineId, $this->logData);
        } catch (\Exception $e) {
            Log::error("Failed to process machine log for machine {$this->machineId}: " . $e->getMessage());
        }
    }
}
