/**
 * @file agent_protocol.h
 * @brief WebSocket 文本协议定义
 */

#ifndef CORE_AGENT_PROTOCOL_H
#define CORE_AGENT_PROTOCOL_H

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

#ifdef __cplusplus
extern "C" {
#endif

/* ---- 下行消息前缀（Server -> Client）---- */

/* ASR */
#define AGENT_PREFIX_ASR                "[Q]:"
#define AGENT_PREFIX_ASR_MID            "[Q]:[M]:"
#define AGENT_PREFIX_ASR_CONT           "[Q]:[C]:"
#define AGENT_PREFIX_ASR_MID_CONT       "[Q]:[M]:[C]:"

/* LLM */
#define AGENT_PREFIX_AI                 "[A]:"
#define AGENT_PREFIX_AI_MID             "[A]:[M]:"
#define AGENT_PREFIX_AI_HINT            "[A]:[H]:"

/* TTS 事件 */
#define AGENT_PREFIX_TTS_BEGIN          "[E]:[TTS_BEGIN_SPEAKING]"
#define AGENT_PREFIX_TTS_END            "[E]:[TTS_END_SPEAKING]"
#define AGENT_PREFIX_TTS_BEGIN_AUDIO    "[E]:[TTS_BEGIN_AUDIO]"
#define AGENT_PREFIX_TTS_END_AUDIO      "[E]:[TTS_END_AUDIO]"

/* 鉴权 */
#define AGENT_PREFIX_LICENSE            "[E]:[LIC]:"
#define AGENT_PREFIX_LICENSE_MUST       "[E]:[LIC]:[MUST]"
#define AGENT_PREFIX_LICENSE_ACTIVE     "[E]:[LIC]:[ACTIVE]:"
#define AGENT_PREFIX_LICENSE_RES        "[E]:[LIC]:[RES]:"
#define AGENT_PREFIX_LICENSE_PASS       "[E]:[LIC]:[RES]:[PASS]:"
#define AGENT_PREFIX_LICENSE_FAILED     "[E]:[LIC]:[RES]:[FAILED]:"

/* 通用事件 */
#define AGENT_PREFIX_EVENT              "[E]:"
#define AGENT_PREFIX_VOICE_COMING       "[E]:[VOICE_COMING]"
#define AGENT_PREFIX_VOICE_DISAPPEAR    "[E]:[VOICE_DISAPPEAR]"
#define AGENT_PREFIX_INTERRUPT          "[E]:[INTERRUPT]"
#define AGENT_PREFIX_VOICE_INTERRUPT    "[E]:[VOICE_INTERRUPT]"
#define AGENT_PREFIX_UPLOAD_IMAGE       "[E]:[UPLOAD_IMAGE]"
#define AGENT_PREFIX_PLAY_AUDIO         "[E]:[PLAY_AUDIO]:"
#define AGENT_PREFIX_CONID              "[E]:[CONID]:"
#define AGENT_PREFIX_AGENTID            "[E]:[AGENTID]:"
#define AGENT_PREFIX_LEAVING            "[E]:[LEAVING]"
#define AGENT_PREFIX_TAG_MSG            "[E]:[TAG_MSG]"
#define AGENT_PREFIX_MEDIA_GEN_START    "[E]:[MEDIA_GENERATE_START]:"
#define AGENT_PREFIX_MEDIA_GEN_RESULT   "[E]:[MEDIA_GENERATE_RESULT]:"
#define AGENT_PREFIX_REMOTE_PLAYER_BEGIN "[E]:[REMOTE_PLAYER_BEGIN]"
#define AGENT_PREFIX_REMOTE_PLAYER_END  "[E]:[REMOTE_PLAYER_END]"
#define AGENT_PREFIX_DIRECT_PLAY        "[E]:[DC]:"

/* FC */
#define AGENT_PREFIX_FUNCTION           "[F]:"

/* 结束标记 */
#define AGENT_PREFIX_DONE               "DONE"

/* ---- 上行命令前缀（Client -> Server）---- */

#define AGENT_CMD_BREAK                 "[B]:"
#define AGENT_CMD_BREAK_BEGIN           "[B]:[BEGIN]:"
#define AGENT_CMD_BREAK_END             "[B]:[END]"

#define AGENT_CMD_TEXT                   "[T]:"
#define AGENT_CMD_TTS_DIRECT            "[TTS]:"

#define AGENT_CMD_AUTO_INT_TRUE         "[SET]:[AUTO_INT]:[TRUE]"
#define AGENT_CMD_AUTO_INT_FALSE        "[SET]:[AUTO_INT]:[FALSE]"
#define AGENT_CMD_DEVICE_INFO           "[SET]:[DEVICE_INFO]:"
#define AGENT_CMD_UPDATE_SYSTEM_PROMPT  "[SET]:[UPDATE_SYSTEM_PROMPT]:"
#define AGENT_CMD_ENHANCE_QUERY         "[SET]:[ENHANCE_QUERY]:"

#define AGENT_CMD_ASR_ENABLE_REALTIME   "[E]:[CMD]:[ASR_ENABLE_REALTIME]"
#define AGENT_CMD_ASR_DISABLE_REALTIME  "[E]:[CMD]:[ASR_DISABLE_REALTIME]"
#define AGENT_CMD_ASR_START_LONGTEXT    "[E]:[CMD]:[ASR_START_LONGTEXT_REC]"
#define AGENT_CMD_ASR_STOP_LONGTEXT     "[E]:[CMD]:[ASR_STOP_LONGTEXT_REC]"

#define AGENT_CMD_REMOTE_PLAYER_STOP    "[E]:[CMD]:[REMOTE_PLAYER]:[STOP]"
#define AGENT_CMD_REMOTE_PLAYER_PAUSE   "[E]:[CMD]:[REMOTE_PLAYER]:[PAUSE]"
#define AGENT_CMD_REMOTE_PLAYER_RESUME  "[E]:[CMD]:[REMOTE_PLAYER]:[RESUME]"

#define AGENT_CMD_MCP_TOOLS_CHANGED     "[E]:[CMD]:[MCP_TOOLS_CHANGED]"
#define AGENT_CMD_SWITCH_SCENE_ROLE     "[OP]:[switchSceneRole]:"

/* ---- 格式化模板 ---- */

#define AGENT_CMD_LICENSE_ACTIVE_FMT    "[E]:[LIC]:[ACTIVE]:{\"devId\":\"%s\",\"uId\":\"%s\",\"licKey\":\"%s\"}"
#define AGENT_CMD_FC_RESULT_FMT         "[F]:{\"session_id\":\"%s\",\"result\":\"%s\",\"message\":\"%s\"}"

/*
 * 设备信息格式：deviceId 必填，否则云端 FC 失效。
 * 参数顺序：deviceId, user_id, asr_text_ext。
 */
#define AGENT_CMD_DEVICE_INFO_FMT       "[SET]:[DEVICE_INFO]:{\"os\":\"rtos\",\"soc\":\"" AIAGENT_DEVICE_SOC "\",\"deviceId\":\"%s\",\"user_id\":\"%s\",\"asr_text_ext\":%s}"

/* ---- 解析后消息类型枚举 ---- */

typedef enum {
    AGENT_MSG_TYPE_UNKNOWN = 0,

    /* ASR */
    AGENT_MSG_TYPE_ASR,
    AGENT_MSG_TYPE_ASR_MID,
    AGENT_MSG_TYPE_ASR_CONT,
    AGENT_MSG_TYPE_ASR_MID_CONT,

    /* LLM */
    AGENT_MSG_TYPE_AI,
    AGENT_MSG_TYPE_AI_MID,
    AGENT_MSG_TYPE_AI_HINT,

    /* TTS */
    AGENT_MSG_TYPE_TTS_BEGIN,
    AGENT_MSG_TYPE_TTS_END,
    AGENT_MSG_TYPE_TTS_BEGIN_AUDIO,
    AGENT_MSG_TYPE_TTS_END_AUDIO,

    /* 鉴权 */
    AGENT_MSG_TYPE_LICENSE,

    /* 事件 */
    AGENT_MSG_TYPE_VOICE_COMING,
    AGENT_MSG_TYPE_VOICE_DISAPPEAR,
    AGENT_MSG_TYPE_INTERRUPT,
    AGENT_MSG_TYPE_UPLOAD_IMAGE,
    AGENT_MSG_TYPE_PLAY_AUDIO,
    AGENT_MSG_TYPE_CONID,
    AGENT_MSG_TYPE_AGENTID,
    AGENT_MSG_TYPE_LEAVING,
    AGENT_MSG_TYPE_TAG_MSG,
    AGENT_MSG_TYPE_MEDIA_GEN_RESULT,
    AGENT_MSG_TYPE_REMOTE_PLAYER_BEGIN,
    AGENT_MSG_TYPE_REMOTE_PLAYER_END,
    AGENT_MSG_TYPE_DIRECT_PLAY,
    AGENT_MSG_TYPE_EVENT,

    /* FC */
    AGENT_MSG_TYPE_FUNCTION,

    /* 结束 */
    AGENT_MSG_TYPE_DONE,
} agent_msg_type_t;

/** 协议解析结果 */
typedef struct {
    agent_msg_type_t type;          /* 消息类型 */
    const char      *content;       /* 前缀之后的内容指针（不拥有）*/
    size_t           content_len;   /* 内容长度 */
    bool             is_final;      /* 是否最终帧 */
} agent_protocol_result_t;

/**
 * @brief 解析文本协议消息
 * @param result 输出解析结果
 * @return 0 成功，负值失败
 */
int agent_protocol_parse_text(const char *buf, size_t len, agent_protocol_result_t *result);

/** @brief 快速前缀类型识别（不做完整解析）*/
agent_msg_type_t agent_protocol_get_type(const char *buf);

#ifdef __cplusplus
}
#endif

#endif /* CORE_AGENT_PROTOCOL_H */
