#pragma once

#include <string>
#include <map>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include "logger.hpp"

using ordered_json = nlohmann::ordered_json;

struct MQTTTopics
{
    std::string uplink_1;
    std::string uplink_2;
    std::string downlink;
};

struct MQTTPorts
{
    int config_port;
    int uplink_1_port;
    int uplink_2_port;
    int downlink_port;
};

struct MQTTConfig
{
    std::string device_id;
    std::string vin;
    std::string server_ip;
    int server_port;
    std::string client_id;
    std::string username;
    std::string password;
    int qos;
    int keep_alive;
    bool clean_session;
    MQTTTopics topics;
    MQTTPorts ports;

    static MQTTConfig load_from_file(const std::string &filepath);
    // 用完整 JSON 对象更新原始配置中已有字段
    bool update_mqtt_config(const ordered_json &new_values,
                            const std::string &filepath = "config.json");
};

// 全局配置变量
extern MQTTConfig g_mqtt_config;
