#pragma once

#include "mqtt_client.h"
#include "mqtt_message_router.h"
#include "mqtt_topics.h"

#include <condition_variable>
#include <memory>
#include <mutex>
#include <string>
#include <thread>

namespace mqtt {

class Service {
public:
    explicit Service(std::string device_id);
    ~Service();

    Service(const Service &) = delete;
    Service &operator=(const Service &) = delete;

    bool start();
    void stop();
    MessageRouter &router();
    int publish_device_message(const std::string &payload, int qos = 0);

private:
    void heartbeat_loop();

    std::string device_id_;
    Topics topics_;
    MessageRouter router_;
    std::unique_ptr<Client> client_;
    std::mutex state_mutex_;
    std::condition_variable wakeup_;
    bool running_ = false;
    int initial_retry_rounds_ = 0;
    std::thread heartbeat_thread_;
};

} // namespace mqtt
