/**
 * @file license.c
 * @brief AI Agent License 认证实现
 */

#include "protocol/license.h"
#include "engine_internal.h"
#include "protocol/agent_protocol.h"
#include "connection/connection_manager.h"
#include "hal_device_info.h"
#include "hal_log.h"
#include <stdio.h>
#include <string.h>

#define TAG "LIC"

int license_activate(engine_internal_ctx_t *ctx,
                     const char *device_id, const char *user_id,
                     const char *license_key)
{
    if (!ctx || !device_id || !license_key) {
        return -1;
    }

    if (!conn_mgr_is_connected(ctx)) {
        BD_LOGE(TAG, "ws not connected");
        return -1;
    }

    /* uId 优先使用外部 user_id，否则取 device_id */
    char uid_buf[64];
    if (user_id && user_id[0]) {
        strncpy(uid_buf, user_id, sizeof(uid_buf) - 1);
        uid_buf[sizeof(uid_buf) - 1] = '\0';
    } else {
        size_t dev_len = strlen(device_id);
        size_t uid_len = dev_len > 16 ? 16 : dev_len;
        memcpy(uid_buf, device_id, uid_len);
        uid_buf[uid_len] = '\0';
    }

    /* 构造 License 激活消息 */
    /* 格式: [E]:[LIC]:[ACTIVE]:{"devId":"xxx","uId":"xxx","licKey":"xxx"} */
    char msg_buf[256];
    int n = snprintf(msg_buf, sizeof(msg_buf),
                     "[E]:[LIC]:[ACTIVE]:{\"devId\":\"%s\",\"uId\":\"%s\",\"licKey\":\"%s\"}",
                     device_id, uid_buf, license_key);
    if (n <= 0 || (size_t)n >= sizeof(msg_buf)) {
        return -1;
    }

    int ret = conn_mgr_send_text(ctx, msg_buf);
    if (ret < 0) {
        BD_LOGE(TAG, "send license activate fail");
        return -1;
    }

    BD_LOGI(TAG, "license activate sent, devId=%s", device_id);
    return 0;
}

/**
 * 解析 License 消息内容(前缀已剥离)
 * content 是 "[E]:[LIC]:" 之后的部分，例如 "[MUST]", "[RES]:[PASS]:xxx", "[ACTIVE]:xxx"
 */
static license_resp_type_t parse_license_content(const char *content, size_t len)
{
    if (!content || len == 0) {
        return LIC_RESP_UNKNOWN;
    }

    if (len >= 6 && strncmp(content, "[MUST]", 6) == 0) {
        return LIC_RESP_MUST;
    }
    if (len >= 12 && strncmp(content, "[RES]:[PASS]", 12) == 0) {
        return LIC_RESP_PASS;
    }
    if (len >= 14 && strncmp(content, "[RES]:[FAILED]", 14) == 0) {
        return LIC_RESP_FAILED;
    }
    if (len >= 8 && strncmp(content, "[ACTIVE]", 8) == 0) {
        return LIC_RESP_ACTIVE_OK;
    }

    return LIC_RESP_UNKNOWN;
}

license_resp_type_t license_parse_response(const char *buf, size_t len)
{
    if (!buf || len == 0) {
        return LIC_RESP_UNKNOWN;
    }

    const char *content = buf;
    size_t content_len = len;

    /* 如果带有前缀则跳过 */
    size_t prefix_len = strlen(AGENT_PREFIX_LICENSE);
    if (len > prefix_len && strncmp(buf, AGENT_PREFIX_LICENSE, prefix_len) == 0) {
        content = buf + prefix_len;
        content_len = len - prefix_len;
    }

    return parse_license_content(content, content_len);
}

bool license_handle_response(const char *buf, size_t len)
{
    license_resp_type_t resp = license_parse_response(buf, len);

    switch (resp) {
    case LIC_RESP_PASS:
        BD_LOGI(TAG, "license PASS");
        return true;
    case LIC_RESP_ACTIVE_OK:
        BD_LOGI(TAG, "license active OK");
        return true;
    case LIC_RESP_MUST:
        BD_LOGI(TAG, "license [MUST] - need activate");
        return false;
    case LIC_RESP_FAILED:
        BD_LOGE(TAG, "license FAILED");
        return false;
    default: {
        BD_LOGB(BD_LOGW, TAG, buf, len, "unknown license resp: %s");
        return false;
    }
    }
}
