<?php

namespace App\Nexus\Console\Commands;

use App\Nexus\Engine\NexusConfig;
use App\Nexus\Engine\SessionController;
use App\Nexus\Engine\SellEvaluator;
use App\Nexus\Engine\EconomyGuardian;
use App\Nexus\Engine\TradeExecutor;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

/**
 * Evaluates all pending sells that have reached the 24-hour evaluation window.
 * Runs every 2 minutes via Laravel scheduler.
 *
 * Fetches the latest session for its criteria profile, then delegates
 * to SellEvaluator::evaluateBatch() which handles the full 9-step
 * evaluation, weighted selection, and trade execution.
 */
class NexusProcessSells extends Command
{
    protected $signature = 'nexus:process-sells';
    protected $description = 'NEXUS: Evaluate all pending sells that have reached 24hr evaluation window';

    public function handle(): void
    {
        if (!NexusConfig::isNexusActive()) return;
        if (NexusConfig::isEmergencyPaused()) return;

        $sessionController = new SessionController();
        $session = $sessionController->getActiveSession();

        $evaluator = new SellEvaluator(
            new EconomyGuardian(),
            new TradeExecutor()
        );

        $result = $evaluator->evaluateBatch($session);

        if ($result['evaluated'] > 0) {
            $this->info("Evaluated: {$result['evaluated']} | Won: {$result['won']} | Lost: {$result['lost']} | Guaranteed: {$result['guaranteed']}");
            Log::info('[NEXUS] process-sells complete', $result);
        }
    }
}
