<?php

namespace App\Console\Commands;

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

class SettleTrades extends Command
{
    protected $signature   = 'trade:settle';
    protected $description = 'Process and settle all expired trades';

    public function handle(TradeService $tradeService): int
    {
        $this->info('Processing expired trades...');

        while (true) {
            try {
                $count = $tradeService->processExpiredTrades();
                if ($count > 0) {
                    $this->info("Settled {$count} trade(s) at " . now()->toTimeString());
                }
            } catch (\Exception $e) {
                $this->error('Error: ' . $e->getMessage());
            }

            sleep(2);
        }

        return self::SUCCESS;
    }
}

// Made with Bob
