#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 MonitoringModule activity classification
 *
 * Workflow:
 * 1. Publish platform:window_changed simulating app switch
 * 2. Wait for monitoring:app_changed (timeout 3s)
 * 3. Validate classification contains app info
 */
class IT_012_MonitoringActivity : public ITestModule {
public:
    std::string getTestName() const override {
        return "IT_012_MonitoringActivity";
    }

    std::string getDescription() const override {
        return "Test MonitoringModule activity classification";
    }

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

        // Subscribe to monitoring events
        grove::SubscriptionConfig subConfig;
        m_io->subscribe("monitoring:app_changed", subConfig);
        m_io->subscribe("monitoring:response", subConfig);

        spdlog::info("[{}] Configured with timeout={}ms", getTestName(), m_timeout);
    }

    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_012_MonitoringActivity"; }
    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("[{}] Simulating window change to VSCode...", getTestName());

            // 1. Simulate window change to a productive app
            auto windowChange = std::make_unique<grove::JsonDataNode>("window");
            windowChange->setString("app", "VSCode");
            windowChange->setString("title", "main.cpp - AISSIA");
            windowChange->setInt("pid", 12345);
            m_io->publish("platform:window_changed", std::move(windowChange));

            // 2. Wait for monitoring:app_changed
            auto appChanged = waitForMessage("monitoring:app_changed", m_timeout);

            if (!appChanged) {
                result.passed = false;
                result.message = "Timeout waiting for monitoring:app_changed";
                return result;
            }

            // 3. Validate response
            std::string app = appChanged->getString("app", "");
            std::string category = appChanged->getString("category", "");

            if (app.empty()) {
                result.passed = false;
                result.message = "No app in monitoring:app_changed event";
                return result;
            }

            result.passed = true;
            result.message = "MonitoringModule classified activity: " + app +
                           (category.empty() ? "" : " (" + category + ")");
            result.details["app"] = app;
            result.details["category"] = category;

            spdlog::info("[{}] Activity classified: {} - {}", getTestName(), app, category);

            // 4. Test query functionality
            std::this_thread::sleep_for(std::chrono::milliseconds(500));

            auto query = std::make_unique<grove::JsonDataNode>("query");
            query->setString("action", "get_current_app");
            m_io->publish("monitoring:query", std::move(query));

            auto queryResponse = waitForMessage("monitoring:response", 2000);
            if (queryResponse) {
                std::string queryApp = queryResponse->getString("current_app", "");
                result.details["query_result"] = queryApp;
                spdlog::info("[{}] Query result: {}", getTestName(), queryApp);
            }

        } 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 = 3000;
};

} // namespace celuna::testing

// Factory functions
extern "C" {
    grove::IModule* createModule() {
        return new celuna::testing::IT_012_MonitoringActivity();
    }

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