<?php

namespace App\Jobs;

use App\Events\PricesUpdatedEvent;
use App\Services\FuelPriceService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

/**
 * Background full station refresh + price poll, dispatched from the admin
 * "Trigger Full Poll" button. Mirrors the `fuel:poll --full` command but
 * calls the service directly so typed exceptions surface to the queue's
 * failed-job handler instead of being swallowed by Artisan output buffering.
 */
class PollFuelPricesJob implements ShouldQueue
{
    use Queueable;

    public function handle(FuelPriceService $service): void
    {
        $service->refreshStations();
        $inserted = $service->pollPrices();

        PricesUpdatedEvent::dispatch($inserted, true);
    }
}
