#pragma once
#include "AiDemoTypes.h"

// WinHTTP WebSocket client used by Step5 AI Transport V2.
// The wire format is intentionally simple:
//   binary request = uint32 little-endian JSON header size + UTF-8 JSON header + media bytes
//   video media bytes = JPEG
//   audio media bytes = PCM16 mono 16 kHz chunks
//   text response  = UTF-8 JSON result from AI service
class AiWebSocketClient
{
public:
    AiWebSocketClient();
    ~AiWebSocketClient();

    bool Connect(const std::wstring& endpoint, std::wstring* errorText);
    void Close();
    bool IsConnected() const;
    const std::wstring& Endpoint() const { return endpoint_; }

    bool SendAnalyzeFrame(int slot,
                          long long timestamp100ns,
                          long long frameIndex,
                          int width,
                          int height,
                          const std::vector<unsigned char>& jpeg,
                          const std::string& modeUtf8,
                          const std::string& promptUtf8,
                          std::string* responseUtf8,
                          std::wstring* errorText);

    bool SendAudioChunk(int audioSource,
                        long long timestamp100ns,
                        long long chunkIndex,
                        int sampleRate,
                        int channels,
                        int bitsPerSample,
                        int sampleCount,
                        const std::vector<unsigned char>& pcm16,
                        const std::string& srcLangUtf8,
                        const std::string& dstLangUtf8,
                        std::string* responseUtf8,
                        std::wstring* errorText);

private:
    bool SendBinary(const std::vector<unsigned char>& payload, std::wstring* errorText);
    bool ReceiveText(std::string* responseUtf8, std::wstring* errorText);

private:
    void* session_;
    void* connect_;
    void* socket_;
    std::wstring endpoint_;
};
