#include "status_mgr.hpp"
#include <iostream>
#include <fstream>
#include <chrono>
#include <string>
#include <json/json.h>
#include <ctime>
#include <iomanip>
#include <zlog.h>

extern zlog_category_t *zbt;

ScheduleStatus get_schedule_status() { 
    std::ifstream status_file("/opt/configenv/status.json");
    if (!status_file.good()) {
        zlog_info(zbt, "[ShortAddrCfg] no file /opt/configenv/status.json");
        return kScheduleStatusNormal;
    }
    Json::Value json_data;
    Json::Reader reader;

    if (reader.parse(status_file, json_data, false)) {
        std::string status = json_data["status"].asString();

        if (status == "debug") {
            status_file.close();
            return kScheduleStatusDebug;
        } else if (status == "normal") {
            status_file.close();
            return kScheduleStatusNormal;
        } else if (status == "upgrade") {
            status_file.close();
            return kScheduleStatusUpgrade;
        }
    }
    status_file.close();    
    return kScheduleStatusNormal;
}

void set_schedule_status(ScheduleStatus status) {
    std::string status_str;
    switch (status) {
        case kScheduleStatusNormal:
            status_str = "normal";
            break;
        case kScheduleStatusDebug:
            status_str = "debug";
            break;
        case kScheduleStatusUpgrade:
            status_str = "upgrade";
            break;
    }

    // Write to status.json
    Json::Value json_data;
    json_data["status"] = status_str;

    std::ofstream status_file("/opt/configenv/status.json");
    if (status_file.is_open()) {
        status_file << json_data;
        status_file.close();
    } else {
        std::cerr << "Unable to open status.json for writing" << std::endl;
    }

    // Append to status_history.json
    Json::Value history_data;
    std::ifstream history_file("/opt/configenv/status_history.json");
    if (history_file.good()) {
        Json::Reader reader;
        reader.parse(history_file, history_data, false);
        history_file.close();
    }    

    // Get the current time
    auto now = std::chrono::system_clock::now();
    auto now_c = std::chrono::system_clock::to_time_t(now);
    std::tm* now_tm = std::localtime(&now_c);

    // Format time as "YYYY-MM-DD HH:MM:SS"
    char time_buffer[20];
    std::strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", now_tm);

    // Add the new status and time to history
    Json::Value new_entry;
    new_entry["status"] = status_str;
    new_entry["time"] = time_buffer;
    history_data.append(new_entry);

    // Write back to status_history.json
    std::ofstream history_file_out("/opt/configenv/status_history.json");
    if (history_file_out.is_open()) {
        history_file_out << history_data;
        history_file_out.close();
    } else {
        std::cerr << "Unable to open status_history.json for writing" << std::endl;
    }
}

std::string get_status_desc(ScheduleStatus status) { 
    std::string status_str;
    switch (status) {
        case kScheduleStatusNormal:
            status_str = "normal";
            break;
        case kScheduleStatusDebug:
            status_str = "debug";
            break;
        case kScheduleStatusUpgrade:
            status_str = "upgrade";
            break;
        default:
            status_str = "normal";
            zlog_error(zbt, "fail to get status desc:%d", status);
            break;
    }
    return status_str;
}
