<?php

namespace App\Listeners\V3\Dashboard\Mission;

use App\Events\V3\Dashboard\Mission\SendDataToFMSEvent;
use App\Services\FMS\GetErrorRateService;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\Client\RequestException;

class SendDataToFMSListener implements ShouldQueue
{
    public function __construct(public GetErrorRateService $getErrorRateService) {}

    /**
     * The name of the queue the job should be sent to.
     *
     * @var string|null
     */
    public ?string $queue = 'fms';

    /**
     * @throws RequestException
     * @throws Exception
     */
    public function handle(SendDataToFMSEvent $event): void
    {
        $mission = $event->mission;

        $this->getErrorRateService->setInputParameters([
            'area' => $mission->area,
            'start_date' => $mission->start_date,
            'end_date' => $mission->end_date,
            'machine_code' => $mission->machines()->first('cmms_machines.machine_code')
        ]);

        $this->getErrorRateService->run();
    }
}
