package thb.jeanluc.adventure.model;

/**
 * Compass directions used for navigating between rooms.
 * Each direction has an opposite, which is used when describing
 * how the player enters the next room (e.g. moving NORTH means
 * entering the next room from the SOUTH).
 */
public enum Direction {
    NORTH,
    SOUTH,
    EAST,
    WEST;

    /**
     * Returns the opposite of this direction.
     *
     * @return the inverse direction
     */
    public Direction getOpposite() {
        return switch (this) {
            case NORTH -> SOUTH;
            case SOUTH -> NORTH;
            case EAST -> WEST;
            case WEST -> EAST;
        };
    }

    /**
     * Parses a direction from a case-insensitive string. Used when
     * loading exits from YAML and when interpreting player input.
     *
     * @param s the direction name; surrounding whitespace is trimmed
     * @return the matching Direction
     * @throws IllegalArgumentException if s is null or does not match a known direction
     */
    public static Direction fromString(String s) {
        if (s == null) {
            throw new IllegalArgumentException("Direction must not be null");
        }
        try {
            return Direction.valueOf(s.trim().toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Unknown direction: '" + s + "'");
        }
    }

    /**
     * Returns the human-readable lowercase label, suitable for display
     * to the player (e.g. "north"). Intentionally not a toString override
     * so that debug logs keep the enum constant form.
     *
     * @return lowercase direction name
     */
    public String getLabel() {
        return name().toLowerCase();
    }
}
