// =========================
// ocpp_api.c
// =========================
#include "ocpp_api.h"
#include "esp_log.h"
#include "cJSON.h"

static const char *TAG = "ocpp_api";

static struct {
    char url[256];
    char chargeBoxId[128];
    char certificate[256];
    char privateKey[256];
} ocpp_config = {"", "", "", ""};

static esp_err_t ocpp_get_status_handler(httpd_req_t *req) {
    httpd_resp_set_type(req, "application/json");
    cJSON *status = cJSON_CreateObject();
    cJSON_AddStringToObject(status, "status", "connected");
    char *str = cJSON_Print(status);
    httpd_resp_sendstr(req, str);
    free(str);
    cJSON_Delete(status);
    return ESP_OK;
}

static esp_err_t ocpp_get_config_handler(httpd_req_t *req) {
    httpd_resp_set_type(req, "application/json");
    cJSON *json = cJSON_CreateObject();
    cJSON_AddStringToObject(json, "url", ocpp_config.url);
    cJSON_AddStringToObject(json, "chargeBoxId", ocpp_config.chargeBoxId);
    cJSON_AddStringToObject(json, "certificate", ocpp_config.certificate);
    cJSON_AddStringToObject(json, "privateKey", ocpp_config.privateKey);
    char *str = cJSON_Print(json);
    httpd_resp_sendstr(req, str);
    free(str);
    cJSON_Delete(json);
    return ESP_OK;
}

static esp_err_t ocpp_post_config_handler(httpd_req_t *req) {
    char buf[512];
    int len = httpd_req_recv(req, buf, sizeof(buf) - 1);
    if (len <= 0) {
        httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Empty body");
        return ESP_FAIL;
    }
    buf[len] = '\0';
    cJSON *json = cJSON_Parse(buf);
    if (!json) {
        httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
        return ESP_FAIL;
    }
    cJSON *url = cJSON_GetObjectItem(json, "url");
    if (url) strlcpy(ocpp_config.url, url->valuestring, sizeof(ocpp_config.url));
    cJSON *id = cJSON_GetObjectItem(json, "chargeBoxId");
    if (id) strlcpy(ocpp_config.chargeBoxId, id->valuestring, sizeof(ocpp_config.chargeBoxId));
    cJSON *cert = cJSON_GetObjectItem(json, "certificate");
    if (cert) strlcpy(ocpp_config.certificate, cert->valuestring, sizeof(ocpp_config.certificate));
    cJSON *key = cJSON_GetObjectItem(json, "privateKey");
    if (key) strlcpy(ocpp_config.privateKey, key->valuestring, sizeof(ocpp_config.privateKey));
    cJSON_Delete(json);
    httpd_resp_sendstr(req, "OCPP config atualizada com sucesso");
    return ESP_OK;
}

void register_ocpp_handlers(httpd_handle_t server, void *ctx) {
    httpd_uri_t status_uri = {
        .uri = "/api/v1/ocpp",
        .method = HTTP_GET,
        .handler = ocpp_get_status_handler,
        .user_ctx = ctx
    };
    httpd_register_uri_handler(server, &status_uri);

    httpd_uri_t get_uri = {
        .uri = "/api/v1/config/ocpp",
        .method = HTTP_GET,
        .handler = ocpp_get_config_handler,
        .user_ctx = ctx
    };
    httpd_register_uri_handler(server, &get_uri);

    httpd_uri_t post_uri = {
        .uri = "/api/v1/config/ocpp",
        .method = HTTP_POST,
        .handler = ocpp_post_config_handler,
        .user_ctx = ctx
    };
    httpd_register_uri_handler(server, &post_uri);
}
