#pragma once
/*
 * turn_client.h — POST /v1/voice/turn to the NPC gateway and retrieve a WAV response.
 *
 * Stage 2: greeting fetch (kind="greeting").
 * Stage 3: reply fetch via /v1/voice/reply (multipart/form-data with captured WAV).
 */
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include "esp_err.h"

#ifdef __cplusplus
extern "C" {
#endif

/*
 * turn_client_greeting — POST /v1/voice/turn with kind="greeting".
 *
 * Sends:  { "session_id": session_id, "number": number, "kind": "greeting" }
 * Bearer: CONFIG_PLIP_GATEWAY_TOKEN (skipped if empty)
 * URL:    CONFIG_PLIP_GATEWAY_URL/v1/voice/turn
 *
 * On success (HTTP 200, body > 44 bytes written to out_path):
 *   - Streams the binary WAV response into fopen(out_path, "wb").
 *   - Returns true.
 *
 * On failure (network error, non-200, short body):
 *   - Logs a warning and returns false.
 *   - Does not crash; caller may proceed silently.
 */
bool turn_client_greeting(const char *session_id,
                           const char *number,
                           const char *out_path);

/*
 * turn_client_reply — POST /v1/voice/reply as multipart/form-data.
 *
 * Endpoint: CONFIG_PLIP_GATEWAY_URL/v1/voice/reply
 * Method:   POST multipart/form-data
 * Fields:   session_id (text), number (text), audio (file, "rec.wav", audio/wav)
 * Bearer:   CONFIG_PLIP_GATEWAY_TOKEN (skipped if empty)
 *
 * The function builds the multipart body in three segments:
 *   preamble  = boundary + session_id part + boundary + number part +
 *               boundary + audio file header
 *   wav data  = wav bytes (wav_len bytes, sent in 4 KB chunks)
 *   epilogue  = CRLF + closing boundary
 *
 * Content-Length = len(preamble) + wav_len + len(epilogue)
 * The response body (WAV 16 kHz) is streamed into out_path.
 *
 * Response headers X-Zacus-Heard and X-Zacus-Said are logged at INFO level.
 *
 * Returns ESP_OK if HTTP 200 and a valid WAV (> 44 bytes) was written.
 * On any failure: logs a warning and returns an ESP error code.
 */
esp_err_t turn_client_reply(const char *session_id,
                             const char *number,
                             const uint8_t *wav,
                             size_t wav_len,
                             const char *out_path);

#ifdef __cplusplus
}
#endif
