#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 NotificationModule alert processing
 *
 * Workflow:
 * 1. Publish scheduler:hyperfocus_alert (triggers notification)
 * 2. Verify NotificationModule processes it (logs will show notification)
 * 3. Validate notification system responds correctly
 *
 * Note: NotificationModule doesn't publish responses, it processes
 * alerts and shows them. This test validates the module accepts
 * and processes the alert without errors.
 */
class IT_011_NotificationAlert : public ITestModule {
public:
    std::string getTestName() const override {
        return "IT_011_NotificationAlert";
    }

    std::string getDescription() const override {
        return "Test NotificationModule alert processing";
    }

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

        // NotificationModule subscribes to these topics
        // We don't need to subscribe ourselves, just send and validate

        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_011_NotificationAlert"; }
    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 hyperfocus alert to NotificationModule...", getTestName());

            // 1. Send hyperfocus alert (triggers URGENT notification)
            auto alert = std::make_unique<grove::JsonDataNode>("alert");
            alert->setString("type", "hyperfocus");
            alert->setInt("duration_minutes", 125);
            alert->setString("task", "IT_011_test_task");
            m_io->publish("scheduler:hyperfocus_alert", std::move(alert));

            // Wait a bit for processing
            std::this_thread::sleep_for(std::chrono::milliseconds(500));

            // 2. Send a break reminder (triggers NORMAL notification)
            auto reminder = std::make_unique<grove::JsonDataNode>("reminder");
            reminder->setString("type", "break");
            reminder->setInt("break_duration", 15);
            m_io->publish("scheduler:break_reminder", std::move(reminder));

            // Wait a bit for processing
            std::this_thread::sleep_for(std::chrono::milliseconds(500));

            // 3. Send AI suggestion (triggers INFO notification)
            auto suggestion = std::make_unique<grove::JsonDataNode>("suggestion");
            suggestion->setString("text", "Test suggestion from IT_011");
            suggestion->setString("category", "productivity");
            m_io->publish("ai:suggestion", std::move(suggestion));

            // Wait for processing
            std::this_thread::sleep_for(std::chrono::milliseconds(500));

            // Since NotificationModule doesn't publish response messages,
            // we validate success by the fact that no errors occurred
            // and the messages were accepted by the IIO system

            result.passed = true;
            result.message = "NotificationModule processed 3 alerts successfully";
            result.details["alerts_sent"] = "hyperfocus + break_reminder + ai_suggestion";

            spdlog::info("[{}] All alerts sent and processed", getTestName());

        } 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:
    grove::IIO* m_io = nullptr;
    grove::ITaskScheduler* m_scheduler = nullptr;
    int m_timeout = 2000;
};

} // namespace celuna::testing

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

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