<?php

namespace App\Console\Commands;

use App\Models\ExchangeRate;
use Illuminate\Console\Command;

class RefreshExchangeRatesCommand extends Command
{
    protected $signature = 'fx:refresh';

    protected $description = 'Fetch the latest USD exchange rates and store them';

    public function handle(): int
    {
        $stored = ExchangeRate::fetchLatest();

        if ($stored === 0) {
            $this->error('Could not fetch exchange rates; the previously stored rates stay in force.');

            return self::FAILURE;
        }

        $this->info("Stored {$stored} exchange rates.");

        return self::SUCCESS;
    }
}
