package thb.jeanluc.adventure.command;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * Tokenising parser for player input. Splits on whitespace, lowercases,
 * then drops a small set of grammatical filler words so that
 * {@code go to the north} parses the same as {@code go north}.
 */
public class CommandParser {

    /**
     * Filler tokens removed from the argument list. Kept deliberately
     * small — when adding more, watch out for words that might be valid
     * item ids ({@code key}, {@code lamp}, etc.).
     */
    private static final Set<String> FILLERS = Set.of("to", "with", "at", "the", "a", "an", "on");

    /**
     * Parses an input line into a verb and its arguments.
     *
     * @param input raw player input; may be null or blank
     * @return parsed command; verb is the empty string if input is blank
     */
    public ParsedCommand parse(String input) {
        if (input == null || input.isBlank()) {
            return new ParsedCommand("", List.of());
        }
        String[] tokens = input.trim().toLowerCase().split("\\s+");
        String verb = tokens[0];
        List<String> args = Arrays.stream(tokens, 1, tokens.length)
                .filter(t -> !FILLERS.contains(t))
                .toList();
        return new ParsedCommand(verb, args);
    }
}
