#include <softbus_core/config/ProfileLoader.h>

#include <fstream>
#include <nlohmann/json.hpp>

namespace softbus::core {

bool DaemonProfile::loadFromFile(const std::string& path)
{
    std::ifstream ifs(path);
    if (!ifs) {
        return false;
    }
    nlohmann::json j;
    ifs >> j;

    if (j.contains("opcua")) {
        opcua.port = static_cast<uint16_t>(j["opcua"].value("port", 4840));
        opcua.applicationName = j["opcua"].value("applicationName", "SoftBusDaemon");
    }
    if (j.contains("uplink")) {
        uplink.listenHost = j["uplink"].value("listenHost", "127.0.0.1");
        uplink.listenPort = static_cast<uint16_t>(j["uplink"].value("listenPort", 9000));
    }
    if (j.contains("plugins")) {
        plugins.modbus = j["plugins"].value("modbus", "protocol_modbus");
        plugins.canopen = j["plugins"].value("canopen", "protocol_canopen");
    }
    if (j.contains("security")) {
        security.tlsEnabled = j["security"].value("tlsEnabled", false);
        security.certPath = j["security"].value("certPath", "");
        security.keyPath = j["security"].value("keyPath", "");
    }
    return true;
}

} // namespace softbus::core
