package com.livingworld.commands;

import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;

import com.livingworld.core.simulation.SimulationManager;

/**
 * Runs a bounded number of forced Living World simulation cycles.
 */
public final class SimulateCommand {

    private SimulateCommand() {
    }

    public static int execute(
            CommandSourceStack source,
            SimulationManager simulationManager,
            int ticks) {
        if (source == null) {
            throw new IllegalArgumentException("source must not be null");
        }
        if (simulationManager == null) {
            throw new IllegalArgumentException("simulationManager must not be null");
        }

        if (ticks <= 0 || ticks > SimulationManager.MAX_FORCED_SIMULATION_TICKS) {
            source.sendFailure(Component.literal(
                    "Ticks must be between 1 and "
                            + SimulationManager.MAX_FORCED_SIMULATION_TICKS + "."));
            return 0;
        }

        try {
            simulationManager.runForcedSimulationTicks(ticks);
            source.sendSuccess(
                    () -> Component.literal(
                            "Ran " + ticks + " Living World simulation ticks."),
                    true);
            return ticks;
        } catch (RuntimeException exception) {
            source.sendFailure(Component.literal(
                    "Simulation failed: " + exception.getMessage()));
            return 0;
        }
    }
}
