#include <shared/testing/ITestModule.h>
#include <grove/JsonDataNode.h>
#include <grove/IIO.h>
#include <spdlog/spdlog.h>
#include <chrono>
#include <thread>

namespace celuna::testing {

/**
 * @brief Test AI → LLM flow with real Claude API
 *
 * Workflow:
 * 1. Publish ai:query with a simple question
 * 2. Wait for llm:response from Claude API
 * 3. Verify response is coherent and not empty
 */
class IT_006_AIToLLM : public ITestModule {
public:
    std::string getTestName() const override {
        return "IT_006_AIToLLM";
    }

    std::string getDescription() const override {
        return "Test AI → LLM with real Claude API";
    }

    void setConfiguration(const grove::IDataNode& config,
                         grove::IIO* io,
                         grove::ITaskScheduler* scheduler) override {
        m_io = io;
        m_scheduler = scheduler;
        m_timeout = config.getInt("timeoutMs", 30000); // 30s for API

        grove::SubscriptionConfig subConfig;
        m_io->subscribe("llm:response", subConfig);
        m_io->subscribe("llm:error", subConfig);

        spdlog::info("[{}] Configured", getTestName());
    }

    void process(const grove::IDataNode& input) override {}
    void shutdown() override {}

    const grove::IDataNode& getConfiguration() override {
        static grove::JsonDataNode config("config");
        return config;
    }

    std::unique_ptr<grove::IDataNode> getHealthStatus() override {
        auto status = std::make_unique<grove::JsonDataNode>("health");
        status->setString("status", "healthy");
        return status;
    }

    std::unique_ptr<grove::IDataNode> getState() override {
        return std::make_unique<grove::JsonDataNode>("state");
    }

    void setState(const grove::IDataNode& state) override {}

    std::string getType() const override { return "IT_006_AIToLLM"; }
    int getVersion() const override { return 1; }
    bool isIdle() const override { return true; }

    TestResult execute() override {
        auto start = std::chrono::steady_clock::now();
        TestResult result;
        result.testName = getTestName();

        try {
            spdlog::info("[{}] Sending query to Claude API...", getTestName());

            // 1. Send simple question to AI
            auto request = std::make_unique<grove::JsonDataNode>("request");
            request->setString("query", "Réponds simplement 'OK' si tu me reçois bien.");
            request->setString("conversationId", "it006");
            m_io->publish("ai:query", std::move(request));

            // 2. Wait for LLM response
            auto response = waitForMessage("llm:response", m_timeout);

            if (!response) {
                // Check for error
                auto error = waitForMessage("llm:error", 1000);
                if (error) {
                    result.passed = false;
                    result.message = "LLM error: " + error->getString("message", "Unknown");
                    result.details["error"] = error->getString("message", "");
                } else {
                    result.passed = false;
                    result.message = "Timeout waiting for llm:response (30s)";
                }
                return result;
            }

            // 3. Validate response
            std::string text = response->getString("text", "");
            std::string conversationId = response->getString("conversationId", "");

            if (text.empty()) {
                result.passed = false;
                result.message = "Empty response from LLM";
                return result;
            }

            if (conversationId != "it006") {
                result.passed = false;
                result.message = "ConversationId mismatch";
                result.details["expected"] = "it006";
                result.details["actual"] = conversationId;
                return result;
            }

            // Check response makes sense (contains "OK" or similar positive response)
            bool coherent = text.find("OK") != std::string::npos ||
                           text.find("oui") != std::string::npos ||
                           text.find("Oui") != std::string::npos ||
                           text.find("reçois") != std::string::npos;

            result.passed = coherent;
            result.message = coherent ?
                "LLM response received and coherent" :
                "LLM responded but answer unexpected";
            result.details["response"] = text;
            result.details["responseLength"] = static_cast<int>(text.length());

            spdlog::info("[{}] Claude response: {}", getTestName(), text.substr(0, 100));

        } catch (const std::exception& e) {
            result.passed = false;
            result.message = std::string("Exception: ") + e.what();
            spdlog::error("[{}] {}", getTestName(), result.message);
        }

        auto end = std::chrono::steady_clock::now();
        result.durationMs = std::chrono::duration_cast<std::chrono::milliseconds>(
            end - start).count();

        return result;
    }

private:
    std::unique_ptr<grove::IDataNode> waitForMessage(
        const std::string& topic, int timeoutMs) {

        auto start = std::chrono::steady_clock::now();

        while (true) {
            if (m_io->hasMessages() > 0) {
                auto msg = m_io->pullMessage();
                if (msg.topic == topic && msg.data) {
                    return std::move(msg.data);
                }
            }

            auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
                std::chrono::steady_clock::now() - start).count();

            if (elapsed > timeoutMs) {
                return nullptr;
            }

            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }
    }

    grove::IIO* m_io = nullptr;
    grove::ITaskScheduler* m_scheduler = nullptr;
    int m_timeout = 30000;
};

} // namespace celuna::testing

extern "C" {
    grove::IModule* createModule() {
        return new celuna::testing::IT_006_AIToLLM();
    }

    void destroyModule(grove::IModule* module) {
        delete module;
    }
}
