#pragma once

// Router: maps "METHOD /path" -> handler. Port of src/jtl-server.js.

#include "http.hpp"
#include "pairing.hpp"
#include "tls_server.hpp"

#include <functional>
#include <string>
#include <unordered_map>

class Router {
public:
    using Handler = RouteHandler;

    void add_route(const std::string& method, const std::string& path, Handler handler);

    // Dispatch a request. Called from the TLS on_request callback.
    void dispatch(tls_session* sess, PairingStore& pairing, const json& config);

    // Suppress init logs for repeated identical requests within 30s
    bool should_log_init(const std::string& url, int status, int duration_ms,
                         const std::string& response);

private:
    std::unordered_map<std::string, Handler> routes_;

    // Init suppression state
    std::string last_init_url_;
    std::string last_init_result_;
    int64_t     last_init_timestamp_ = 0;
    int         suppressed_count_ = 0;
    int64_t     suppressed_report_ts_ = 0;
};
