#pragma once

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Connect to a WebSocket server.
 * @param host Server IP address (e.g. "192.168.68.126")
 * @param port Port number (e.g. 8642)
 * @param path WebSocket path (e.g. "/api/esp32/voice/ws")
 * @param device_id Unique device identifier
 * @param auth_key Hermes Bearer API key
 * @return Socket file descriptor on success, or -1 on failure
 */
int ws_connect(const char* host, int port, const char* path, const char* device_id, const char* auth_key);

/**
 * Send a WebSocket frame.
 * @param fd Socket file descriptor
 * @param data Data payload to send
 * @param len Length of the data payload
 * @param binary True for binary frame, false for text frame
 * @return 0 on success, or -1 on failure
 */
int ws_send(int fd, const uint8_t* data, size_t len, bool binary);

/**
 * Receive a WebSocket frame.
 * @param fd Socket file descriptor
 * @param out_opcode Pointer to store the received opcode (e.g. 0x01 text, 0x02 binary)
 * @param payload Buffer to store the received payload
 * @param max_len Maximum length of the payload buffer
 * @return Received payload length on success, -1 on connection failure, or -2 on buffer overflow
 */
int ws_recv(int fd, int* out_opcode, uint8_t* payload, size_t max_len);

/**
 * Close a WebSocket connection.
 * @param fd Socket file descriptor
 */
void ws_close(int fd);

/**
 * Send a WebSocket PONG frame.
 * @param fd Socket file descriptor
 * @param payload Payload to reflect
 * @param len Length of payload
 * @return 0 on success, or -1 on failure
 */
int ws_send_pong(int fd, const uint8_t* payload, size_t len);

#ifdef __cplusplus
}
#endif
