/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2026 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "jmap_methods.h"

CxBuffer* jm_mailbox_get_request(cxstring account_id) {
    CxBuffer *buf = cxBufferCreate(NULL, NULL, 512, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS);
    
    cxBufferPutString(buf,
            "{\"using\":["
                "\"urn:ietf:params:jmap:core\","
                "\"urn:ietf:params:jmap:mail\""
            "],\"methodCalls\":["
                "[\"Mailbox/get\","
                    "{\"accountId\":\"");
    cxBufferPutString(buf, account_id);
    cxBufferPutString(buf,
            "\",\"ids\":null},"
                    "\"0\""
                "]"
            "]}");
    
    return buf;
}

CxJsonValue* jm_do_api_request(JMAPSession *sn, CxBuffer *request) {
    curl_easy_setopt(sn->handle, CURLOPT_URL, sn->apiUrl);
    
    curl_easy_setopt(sn->handle, CURLOPT_POST, 1L);
    curl_easy_setopt(sn->handle, CURLOPT_READFUNCTION, cxBufferRead);
    curl_easy_setopt(sn->handle, CURLOPT_SEEKFUNCTION, cxBufferSeek);
    curl_easy_setopt(sn->handle, CURLOPT_READDATA, request);
    curl_easy_setopt(sn->handle, CURLOPT_SEEKDATA, request);
    curl_easy_setopt(sn->handle, CURLOPT_INFILESIZE, request->size);
    
    CxBuffer *response = cxBufferCreate(NULL, NULL, 4096, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS);
    curl_easy_setopt(sn->handle, CURLOPT_WRITEFUNCTION, cxBufferWrite);
    curl_easy_setopt(sn->handle, CURLOPT_WRITEDATA, response);
    
    CURLcode ret = curl_easy_perform(sn->handle);
    long http_status;
    curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
    CxJsonValue *json = NULL;
    if(ret == CURLE_OK) {
        if(http_status >= 200 && http_status <= 299) {
            (void)cx_json_from_string(sn->mp->allocator, cx_strn(response->space, response->size), &json);
        }
    }
    cxBufferFree(response);
    
    return json;
}
