#include "r1_voice/conversation.hh"

#include <cstdlib>
#include <iostream>
#include <string>

static void printUsage(
    const char* program)
{
    std::cerr
        << "Usage:\n\n"
        << "  "
        << program
        << " <network_interface> "
        << "[whisper_cli] "
        << "[whisper_model] "
        << "[llm_url]\n\n"

        << "Example:\n\n"

        << "  "
        << program
        << " enp3s0 "
        << "/home/jvizner/esarobotech/whisper.cpp/build/bin/whisper-cli "
        << "/home/jvizner/esarobotech/whisper.cpp/models/ggml-medium.bin "
        << "http://127.0.0.1:8080/v1/chat/completions\n";
}

int main(
    int argc,
    char** argv)
{
    if (argc < 2) {
        printUsage(argv[0]);
        return EXIT_FAILURE;
    }

    const std::string interface =
        argv[1];

    r1_voice::ConversationConfig config;

    // ---------------------------------------------------------
    // Whisper STT
    // ---------------------------------------------------------

    config.stt.whisper_cli =
        argc >= 3
            ? argv[2]
            : "/home/jvizner/esarobotech/whisper.cpp/build/bin/whisper-cli";

    config.stt.whisper_model =
        argc >= 4
            ? argv[3]
            : "/home/jvizner/esarobotech/whisper.cpp/models/ggml-medium.bin";

    // Czech speech
    config.stt.language = "cs";

    config.stt.threads = 8;

    // Better protection against clipping the first words.
    config.stt.speech_threshold = 0.010f;
    config.stt.preroll_ms = 800;

    config.stt.min_speech_ms = 250;
    config.stt.silence_ms = 900;
    config.stt.max_record_ms = 10000;

    // ---------------------------------------------------------
    // Qwen LLM
    // ---------------------------------------------------------

    config.llm.url =
        argc >= 5
            ? argv[4]
            : "http://127.0.0.1:8080/v1/chat/completions";

    config.llm.model = "local";

    config.llm.temperature = 0.4f;
    config.llm.max_tokens = 80;

    config.llm.system_prompt =
        "Jsi ESO 2, humanoidní robot. "
        "Mluv česky, pokud uživatel mluví česky. "
        "Odpovídej stručně a přirozeně, obvykle jednou až dvěma větami. "
        "Pokud je vstup zjevně nesrozumitelný nebo poškozený rozpoznáváním řeči, "
        "nevymýšlej jeho význam a jednoduše požádej uživatele, aby větu zopakoval. "
        "Neopakuj stále stejnou odpověď. "
        "Nepoužívej Markdown. "
        "Nevypisuj uvažování ani <think> bloky.";

    // ---------------------------------------------------------
    // Czech Piper TTS
    // ---------------------------------------------------------

    config.tts.model =
        "/home/jvizner/esarobotech/piper/cs_medium.onnx";

    config.tts.output_wav =
        "/tmp/r1_tts.wav";

    config.tts.volume = 100;

    config.tts.app_name =
        "example";

    // ---------------------------------------------------------
    // Conversation
    // ---------------------------------------------------------

    r1_voice::Conversation conversation(
        config
    );

    if (!conversation.init(interface)) {
        std::cerr
            << "Failed to initialize voice system\n";

        return EXIT_FAILURE;
    }

    conversation.run();

    return EXIT_SUCCESS;
}
