#pragma once
#include <functional>
#include <string>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <atomic>
#include <vector>

class MessageBroker {
public:
    using MessageCallback = std::function<void(const std::string &message)>;

    void publish(const std::string &message);
    void subscribe(const MessageCallback &callback);
    void start();
    void stop();

private:
    std::queue<std::string> messageQueue;
    std::vector<MessageCallback> subscribers;
    std::mutex mutex;
    std::condition_variable cv;
    std::atomic<bool> running{false};
    std::thread brokerThread;

    void processMessages();
};
