#include "WebServer.hpp"
#include "../SdCard.hpp"
#include "esp_log.h"
#include <cstring>

static const char* TAG = "WebServer";

esp_err_t WebServer::postUsbModeHandler(httpd_req_t* req) {
    ESP_LOGI(TAG, "Enabling USB Mass Storage mode");

    bool success = SdCard::instance().enableUsbMsc();

    cJSON* json = cJSON_CreateObject();
    if (success) {
        cJSON_AddStringToObject(json, "status", "ok");
        cJSON_AddStringToObject(json, "message", "USB MSC enabled. Connect USB cable to access SD card. Reboot to return to normal mode.");
    } else {
        cJSON_AddStringToObject(json, "status", "error");
        cJSON_AddStringToObject(json, "message", "Failed to enable USB MSC");
    }
    return sendJsonObject(req, json);
}

esp_err_t WebServer::getStatusHandler(httpd_req_t* req) {
    cJSON* json = cJSON_CreateObject();
    cJSON_AddBoolToObject(json, "sdMounted", SdCard::instance().isMounted());
    cJSON_AddBoolToObject(json, "usbMscActive", SdCard::instance().isUsbMscActive());
    return sendJsonObject(req, json);
}
