import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class Capitulo10Chatbot {
    private final String nome;
    private final List<String> historico = new ArrayList<>();

    public Capitulo10Chatbot(String nome) {
        if (nome == null || nome.isBlank()) {
            throw new IllegalArgumentException("Nome obrigatório.");
        }
        this.nome = nome.strip();
    }

    static String normalizar(String texto) {
        return texto == null ? "" : texto.strip().toLowerCase(Locale.ROOT);
    }

    public String responder(String entrada) {
        String comando = normalizar(entrada);
        if (comando.isBlank()) {
            return "Digite uma mensagem.";
        }
        if (comando.equals("historico")) {
            return historico.isEmpty()
                    ? "Sem histórico."
                    : String.join(System.lineSeparator(), historico);
        }

        String resposta = switch (comando) {
            case "oi" -> "Olá! Eu sou " + nome + ".";
            case "ajuda" -> "Comandos: oi, ajuda, dica, historico e sair.";
            case "dica" -> "Compile e teste depois de cada pequena mudança.";
            default -> "Não entendi. Digite ajuda.";
        };

        historico.add("USUÁRIO: " + entrada.strip());
        historico.add("BOT: " + resposta);
        return resposta;
    }

    public int totalInteracoes() {
        return historico.size() / 2;
    }

    public static void main(String[] args) {
        Capitulo10Chatbot bot = new Capitulo10Chatbot("TutorJavaBot");
        System.out.println(bot.responder("oi"));
        System.out.println(bot.responder("historico"));
        System.out.println("Interações: " + bot.totalInteracoes());
    }
}
