#pragma once

#include "PeerAction.hpp"
#include "Peer.pb.h"
#include "Address.hpp"

#include <cstdint>
#include <functional>

namespace tw::p2p {

// Node-level peer-networking abstraction.
//
// Owns its transport (QUIC endpoint or TCP listener), manages all peer
// connections, and internalises the PeerHello/PeerBye handshake.
//
// External code only sees:
//   on_connected(peer_id)   — handshake with peer_id complete (both hellos sent)
//   on_action(peer_id, a)   — PeerAction received from peer_id
//   connect_to(peer_id, a)  — initiate outgoing connection
//   send_action(peer_id, m) — unreliable send to a connected peer
//   poll()                  — pump I/O; must be called regularly
class PeerLink {
public:
    using ConnectedHandler = std::function<void(uint32_t peer_id)>;
    using ActionHandler    = std::function<void(uint32_t peer_id, const PeerAction& action)>;

    virtual ~PeerLink() = default;

    void on_connected(ConnectedHandler h) { m_connected_handler = std::move(h); }
    void on_action   (ActionHandler    h) { m_action_handler    = std::move(h); }

    virtual void connect_to (uint32_t peer_id, const tw::net::Address& addr) = 0;
    virtual void send_batch (uint32_t peer_id, const mmo::peer::PeerActionBatch& batch) = 0;
    virtual void poll       () = 0;

protected:
    ConnectedHandler m_connected_handler;
    ActionHandler    m_action_handler;
};

} // namespace tw::p2p
