#ifndef MVGAL_TRANSPORT_H
#define MVGAL_TRANSPORT_H

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "mvgal/mvgal_network.h"

#ifdef __cplusplus
extern "C" {
#endif

/* ── Transport configuration ─────────────────────────────────────────── */

typedef struct mvgal_transport_config {
    uint16_t tcp_port;               /* TCP data port (0 = ephemeral) */
    const char *bind_addr;           /* Bind address (NULL = INADDR_ANY) */
    uint32_t heartbeat_interval_ms;  /* Heartbeat interval (0 = default 5000) */
    uint32_t timeout_ms;             /* Peer timeout (0 = default 30000) */
    uint32_t max_peers;              /* Max concurrent peers (0 = default 64) */
    size_t recv_buffer_size;         /* Per-connection recv buf (0 = default 65536) */
    bool enable_heartbeat;           /* Enable automatic heartbeat */
} mvgal_transport_config_t;

#define MVGAL_TRANSPORT_DEFAULT_CONFIG { \
    .tcp_port = 0,                         \
    .bind_addr = NULL,                     \
    .heartbeat_interval_ms = 5000,         \
    .timeout_ms = 30000,                   \
    .max_peers = 64,                       \
    .recv_buffer_size = 65536,             \
    .enable_heartbeat = true               \
}

/* ── Transport statistics ────────────────────────────────────────────── */

typedef struct mvgal_transport_stats {
    uint64_t bytes_sent;
    uint64_t bytes_received;
    uint64_t messages_sent;
    uint64_t messages_received;
    uint64_t messages_dropped;
    uint32_t active_connections;
    uint32_t total_connections;
    uint32_t connection_errors;
    uint32_t heartbeat_timeouts;
} mvgal_transport_stats_t;

/* ── Callbacks ───────────────────────────────────────────────────────── */

/* Called when a complete message is received from a peer */
typedef void (*mvgal_transport_msg_callback)(uint64_t node_id,
                                              const mvgal_net_message_header_t *hdr,
                                              const void *payload,
                                              void *userdata);

/* Called when a peer connects or disconnects */
typedef void (*mvgal_transport_peer_callback)(uint64_t node_id,
                                               mvgal_net_peer_state_t state,
                                               void *userdata);

/* ── Lifecycle ───────────────────────────────────────────────────────── */

int mvgal_transport_init(const mvgal_transport_config_t *config);
void mvgal_transport_shutdown(void);

/* ── Server ──────────────────────────────────────────────────────────── */

int mvgal_transport_listen(void);
int mvgal_transport_stop(void);

/* ── Client connections ──────────────────────────────────────────────── */

int mvgal_transport_connect(const char *addr, uint16_t port, uint64_t node_id);
int mvgal_transport_disconnect(uint64_t node_id);
int mvgal_transport_disconnect_all(void);

/* ── Send / receive ──────────────────────────────────────────────────── */

int mvgal_transport_send(uint64_t node_id,
                          const mvgal_net_message_header_t *hdr,
                          const void *payload,
                          size_t payload_size);

int mvgal_transport_broadcast(const mvgal_net_message_header_t *hdr,
                               const void *payload,
                               size_t payload_size);

/* ── Polling / event loop ────────────────────────────────────────────── */

int mvgal_transport_poll(uint32_t timeout_ms);

/* ── Queries ─────────────────────────────────────────────────────────── */

bool mvgal_transport_is_connected(uint64_t node_id);
int  mvgal_transport_get_peer(uint64_t node_id, mvgal_net_peer_t *peer);
int  mvgal_transport_get_peers(mvgal_net_peer_t *peers, uint32_t *count);
void mvgal_transport_get_stats(mvgal_transport_stats_t *stats);

/* ── Callback registration ───────────────────────────────────────────── */

void mvgal_transport_set_msg_callback(mvgal_transport_msg_callback cb, void *userdata);
void mvgal_transport_set_peer_callback(mvgal_transport_peer_callback cb, void *userdata);

#ifdef __cplusplus
}
#endif

#endif /* MVGAL_TRANSPORT_H */
