#pragma once

#include <grove/IModule.h>
#include <grove/JsonDataNode.h>

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <memory>
#include <string>
#include <queue>

namespace celuna {

/**
 * @brief Notification Module - Alertes système et TTS
 *
 * Fonctionnalités:
 * - Notifications système (console pour l'instant, Windows toast plus tard)
 * - File d'attente des notifications avec priorités
 * - Support multilingue (FR/EN/JP selon config)
 * - Mode silencieux configurable
 *
 * Souscrit à:
 * - "scheduler/hyperfocus_alert"  : Alerte hyperfocus -> notification urgente
 * - "scheduler/break_reminder"    : Rappel pause -> notification normale
 * - "ai/suggestion"               : Suggestion IA -> notification info
 * - "language/correction"         : Correction langue -> notification légère
 */
class NotificationModule : public grove::IModule {
public:
    enum class Priority {
        LOW,      // Info, peut être ignorée
        NORMAL,   // Rappels standards
        HIGH,     // Alertes importantes
        URGENT    // Hyperfocus, urgences
    };

    struct Notification {
        std::string id;
        std::string title;
        std::string message;
        Priority priority;
        std::string language;  // "fr", "en", "jp"
        bool read;
        float timestamp;
    };

    NotificationModule();
    ~NotificationModule() override = default;

    // IModule interface
    void process(const grove::IDataNode& input) override;
    void setConfiguration(const grove::IDataNode& configNode, grove::IIO* io,
                         grove::ITaskScheduler* scheduler) override;
    const grove::IDataNode& getConfiguration() override;
    std::unique_ptr<grove::IDataNode> getHealthStatus() override;
    void shutdown() override;
    std::unique_ptr<grove::IDataNode> getState() override;
    void setState(const grove::IDataNode& state) override;
    std::string getType() const override { return "NotificationModule"; }
    bool isIdle() const override { return true; }
    int getVersion() const override { return 1; }

    // API publique
    void notify(const std::string& title, const std::string& message, Priority priority = Priority::NORMAL);

private:
    // Configuration
    std::string m_language = "fr";
    bool m_silentMode = false;
    bool m_ttsEnabled = false;  // Text-to-Speech (future)
    int m_maxQueueSize = 50;

    // État
    std::queue<Notification> m_pendingNotifications;
    int m_notificationCount = 0;
    int m_urgentCount = 0;

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

    // Helpers
    void processNotificationQueue();
    void displayNotification(const Notification& notif);
    std::string priorityToString(Priority p);
    std::string priorityToEmoji(Priority p);
};

} // namespace celuna

extern "C" {
    grove::IModule* createModule();
    void destroyModule(grove::IModule* module);
}
