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

namespace fs = std::filesystem;

namespace celuna::testing {

/**
 * @brief Test MCP tool filesystem_write via AIModule
 *
 * Workflow:
 * 1. Publish ai:query requesting file write
 * 2. Wait for llm:response (timeout 30s)
 * 3. Verify file was created with correct content
 */
class IT_002_FileSystemWrite : public ITestModule {
public:
    std::string getTestName() const override {
        return "IT_002_FileSystemWrite";
    }

    std::string getDescription() const override {
        return "Test MCP tool filesystem_write";
    }

    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);

        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_002_FileSystemWrite"; }
    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();

        const std::string testFile = "data/test_output_it002.md";
        const std::string testContent = "Test IT_002 réussi - Integration test write";

        try {
            // Cleanup previous test file if exists
            if (fs::exists(testFile)) {
                fs::remove(testFile);
                spdlog::info("[{}] Cleaned up previous test file", getTestName());
            }

            spdlog::info("[{}] Requesting file write via AI...", getTestName());

            // 1. Send query to AI
            auto request = std::make_unique<grove::JsonDataNode>("request");
            request->setString("query",
                "Utilise le tool filesystem_write pour écrire le texte '" +
                testContent + "' dans le fichier '" + testFile + "'");
            request->setString("conversationId", "it002");
            m_io->publish("ai:query", std::move(request));

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

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

            std::string text = response->getString("text", "");
            spdlog::info("[{}] LLM response: {}", getTestName(), text);

            // 3. Give LLM time to execute tool (async)
            std::this_thread::sleep_for(std::chrono::milliseconds(2000));

            // 4. Verify file was created
            if (!fs::exists(testFile)) {
                result.passed = false;
                result.message = "File was not created: " + testFile;
                result.details["response"] = text;
                return result;
            }

            // 5. Verify content
            std::ifstream file(testFile);
            std::string content((std::istreambuf_iterator<char>(file)),
                               std::istreambuf_iterator<char>());
            file.close();

            bool contentMatches = content.find(testContent) != std::string::npos;

            result.passed = contentMatches;
            result.message = contentMatches ?
                "File created with correct content" :
                "File created but content mismatch";
            result.details["filePath"] = testFile;
            result.details["expectedContent"] = testContent;
            result.details["actualContent"] = content;

            // Cleanup
            if (fs::exists(testFile)) {
                fs::remove(testFile);
            }

        } 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_002_FileSystemWrite();
    }

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