<?php

namespace App\Console\Commands;

use App\Services\ExchangeRateService;
use Illuminate\Console\Command;

class UpdateExchangeRates extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'exchange:update';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        // dd('Command is running at ' . now());
        // Get settings from DB
        $autoUpdate = setting('exchange_rate_update_auto'); // true/false
        $updateTime = setting('exchange_rate_update_time'); // e.g. "14:30"

        if (!$autoUpdate) {
            return;
        }

        // Check time match (optional if using scheduler timing)
        if (now()->format('H:i') !== $updateTime) {
            return;
        }

        $result = ExchangeRateService::addRecord();

        if (!$result) {
            throw new \Exception('Exchange rate update failed');
        }
    }
}
