/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * MindIE is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
#include "safe_io.h"

#include <fstream>
#include <iostream>

#include "config_info.h"
#include "safe_path.h"

namespace mindie_llm {

static int g_jsonDepthLimit = JSON_DEPTH_LIMIT_MIN;  // 限制嵌套层次

void SetJsonDepthLimit(int depth) { g_jsonDepthLimit = depth; }

int GetJsonDepthLimit() { return g_jsonDepthLimit; }

Result LoadJson(const std::string& path, Json& json) {
    std::string checkedPath;
    SafePath inFile(path, PathType::FILE, "r", PERM_640, SIZE_500MB, ".json");
    Result r = inFile.Check(checkedPath);
    if (!r.IsOk()) {
        return r;
    }
    std::ifstream file(checkedPath);
    if (!file.is_open()) {
        return Result::Error(ResultCode::IO_FAILURE, "Failed to open file: " + checkedPath);
    }
    json = nlohmann::json::parse(file, CheckJsonDepthCallbackNoLogger);
    return Result::OK();
}

bool CheckJsonDepth(int depth, Json::parse_event_t ev) {
    switch (ev) {
        case Json::parse_event_t::object_start:
        case Json::parse_event_t::array_start:
            return depth <= g_jsonDepthLimit;
        default:
            return true;
    }
}

bool CheckJsonDepthWithLogger(int depth, Json::parse_event_t ev, std::function<void(void)> logger) {
    if (!CheckJsonDepth(depth, ev)) {
        if (logger) {
            logger();
        }
        return false;
    }
    return true;
}

bool CheckJsonDepthCallbackNoLogger(int depth, Json::parse_event_t ev, [[maybe_unused]] Json& obj) {
    return CheckJsonDepthWithLogger(depth, ev, [depth]() {
        std::cerr << "Failed to parse json: depth is " << depth << ", limit is " << GetJsonDepthLimit();
    });
}

}  // namespace mindie_llm