#pragma once
#include <grove/IModule.h>
#include <memory>
#include <spdlog/spdlog.h>

namespace celuna {

/**
 * @brief WebModule - Handles HTTP requests via IIO pub/sub
 *
 * Provides HTTP GET/POST capabilities to other modules through the IIO system.
 * Modules publish to "web:request" and receive responses on "web:response".
 *
 * Features:
 * - GET/POST/PUT/DELETE support
 * - Custom headers and body
 * - Timeout configuration
 * - Request statistics tracking
 * - Hot-reload state preservation
 */
class WebModule : public grove::IModule {
public:
    WebModule();
    ~WebModule() override = default;

    // IModule interface
    std::string getType() const override { return "WebModule"; }
    bool isIdle() const override { return true; }

    void setConfiguration(const grove::IDataNode& config,
                         grove::IIO* io,
                         grove::ITaskScheduler* scheduler) override;
    const grove::IDataNode& getConfiguration() override;
    void process(const grove::IDataNode& input) override;
    std::unique_ptr<grove::IDataNode> getHealthStatus() override;
    void shutdown() override;

    // State management (hot-reload)
    std::unique_ptr<grove::IDataNode> getState() override;
    void setState(const grove::IDataNode& state) override;

private:
    void processMessages();
    void handleWebRequest(const grove::IDataNode& request);
    bool isUrlAllowed(const std::string& url);

    grove::IIO* m_io = nullptr;
    std::shared_ptr<spdlog::logger> m_logger;
    std::unique_ptr<grove::IDataNode> m_config;

    // Configuration
    bool m_enabled = true;
    int m_requestTimeoutMs = 30000;
    int m_maxConcurrentRequests = 10;

    // Statistics (preserved on hot-reload)
    int m_totalRequests = 0;
    int m_successfulRequests = 0;
    int m_failedRequests = 0;
};

} // namespace celuna
