package thb.jeanluc.adventure.command.impl;

import lombok.RequiredArgsConstructor;
import thb.jeanluc.adventure.command.Command;
import thb.jeanluc.adventure.command.CommandRegistry;
import thb.jeanluc.adventure.game.GameContext;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Lists all available commands and their short help text. Pulls the
 * command set from the {@link CommandRegistry} so it stays in sync
 * automatically. Usage: {@code help}.
 */
@RequiredArgsConstructor
public class HelpCommand implements Command {

    /** Registry that owns the commands to be listed. */
    private final CommandRegistry registry;

    @Override
    public void execute(GameContext ctx, List<String> args) {
        String body = registry.distinctCommands().stream()
                .map(Command::help)
                .collect(Collectors.joining("\n  "));
        ctx.getIo().write("Available commands:\n  " + body);
    }

    @Override
    public String help() {
        return "help  -  show this list (alias: ?)";
    }
}
