<?php

namespace App\Console\Commands;

use App\Services\Forecasting\BeisImporter;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Throwable;

#[Signature('beis:import')]
#[Description('Pull the latest gov.uk Weekly road fuel prices CSV and upsert into weekly_pump_prices.')]
class ImportBeisFuelPrices extends Command
{
    public function handle(BeisImporter $importer): int
    {
        try {
            $result = $importer->import();
        } catch (Throwable $e) {
            $this->error('BEIS import failed: '.$e->getMessage());

            return self::FAILURE;
        }

        $this->info(sprintf(
            'Imported %d rows from %s — latest date: %s.',
            $result['parsed'],
            $result['csv_url'],
            $result['latest_date'],
        ));
        $this->info('Forecast cache flushed; next API hit will retrain on the new row.');

        return self::SUCCESS;
    }
}
