#pragma once
#include <condition_variable>
#include <chrono>
#include <cstdint>
#include <mutex>
#include <thread>
#include "dispatcher.hpp"
#include "runtime_state.hpp"
#include "telegram_client.hpp"
namespace telegrammer
{
/// Long-polls Telegram and commits updates before advancing the offset.
class Poller
{
public:
/// Construct a poller for one bot identity and persistent state database.
Poller(TelegramApi& client, DeliveryStore& store,
Dispatcher& dispatcher, RuntimeState& state, int64_t bot_id);
/// Start the polling thread.
void start();
/// Set the validated Telegram bot identity before polling starts.
void setBotId(int64_t bot_id);
/// Request polling to stop and wait for the thread.
void stop();
private:
TelegramApi& client_;
DeliveryStore& store_;
Dispatcher& dispatcher_;
RuntimeState& state_;
int64_t bot_id_;
std::mutex mutex_;
std::condition_variable_any condition_;
std::jthread worker_;
void run(std::stop_token stop_token);
bool wait(std::stop_token stop_token,
std::chrono::milliseconds duration);
};
} // namespace telegrammer