/**
 * @file userserver_client.c
 * @brief UserServer HTTP 客户端实现
 */

#include "connection/userserver_client.h"
#include "hal_http.h"
#include "hal_json.h"
#include "hal_log.h"
#include <stdio.h>
#include <string.h>

#define TAG  "USRSVR"

#define URL_BUF_SIZE          256
#define BODY_BUF_SIZE         2048

int userserver_create_instance(const char *base_url, const char *path,
                               const char *app_id, const char *config,
                               userserver_session_t *out)
{
    if (!base_url || !base_url[0] || !path || !path[0] ||
        !app_id || !app_id[0] || !config || !out) {
        return -1;
    }

    char url[URL_BUF_SIZE];
    int n = snprintf(url, sizeof(url), "%s%s", base_url, path);
    if (n <= 0 || n >= (int)sizeof(url)) {
        BD_LOGE(TAG, "url too long");
        return -1;
    }

    /* config 已是合法 JSON 对象字符串，直接拼装 */
    char body[BODY_BUF_SIZE];
    n = snprintf(body, sizeof(body),
                 "{\"app_id\":\"%s\",\"config\":%s}",
                 app_id, config);
    if (n <= 0 || n >= (int)sizeof(body)) {
        BD_LOGE(TAG, "body too long");
        return -1;
    }

    hal_http_handle_t http = hal_http_create();
    if (!http) {
        return -1;
    }

    hal_http_response_t resp = {0};
    BD_LOGI(TAG, "create: %s", url);
    int ret = hal_http_post(http, url, "application/json", body, (size_t)n, &resp);
    if (ret < 0 || !resp.body || resp.body_len == 0) {
        BD_LOGE(TAG, "create http fail: %d", ret);
        hal_http_response_free(&resp);
        hal_http_destroy(http);
        return -1;
    }

    /* hal_http_post 接收缓冲区固定 4KB，UserServer 响应远小于此 */
    resp.body[resp.body_len] = '\0';

    int rc = 0;
    memset(out, 0, sizeof(*out));

    hal_json_t json = hal_json_parse(resp.body);
    if (!json) {
        BD_LOGE(TAG, "json parse fail");
        rc = -1;
    } else {
        /* instance_id 按字符串读取 */
        if (hal_json_get_number_string(json, "ai_agent_instance_id",
                                       out->instance_id, sizeof(out->instance_id)) < 0) {
            BD_LOGE(TAG, "missing instance_id");
            rc = -1;
        } else {
            const char *tok = hal_json_get_string(json, "token");
            if (!tok) {
                BD_LOGE(TAG, "missing token");
                rc = -1;
            } else {
                strncpy(out->token, tok, sizeof(out->token) - 1);
                out->token[sizeof(out->token) - 1] = '\0';
                BD_LOGI(TAG, "instance=%s", out->instance_id);
            }
        }
        hal_json_destroy(json);
    }

    hal_http_response_free(&resp);
    hal_http_destroy(http);
    return rc;
}

int userserver_destroy_instance(const char *base_url, const char *path,
                                const char *app_id, const char *instance_id)
{
    if (!base_url || !base_url[0] || !path || !path[0] ||
        !app_id || !app_id[0] || !instance_id || !instance_id[0]) {
        return -1;
    }

    char url[URL_BUF_SIZE];
    int n = snprintf(url, sizeof(url), "%s%s", base_url, path);
    if (n <= 0 || n >= (int)sizeof(url)) {
        return -1;
    }

    char body[256];
    n = snprintf(body, sizeof(body),
                 "{\"app_id\":\"%s\",\"ai_agent_instance_id\":%s}",
                 app_id, instance_id);
    if (n <= 0 || n >= (int)sizeof(body)) {
        return -1;
    }

    hal_http_handle_t http = hal_http_create();
    if (!http) {
        return -1;
    }

    hal_http_response_t resp = {0};
    BD_LOGI(TAG, "destroy %s", instance_id);
    int ret = hal_http_post(http, url, "application/json", body, (size_t)n, &resp);
    hal_http_response_free(&resp);
    hal_http_destroy(http);

    if (ret < 0) {
        BD_LOGW(TAG, "destroy http fail: %d", ret);
        return -1;
    }
    return 0;
}
