/**
* Nudsf_DataRepository
* Nudsf Data Repository Service. © 2021, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved. 
*
* The version of the OpenAPI document: 1.1.0-alpha.2
* 
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
#include "udsf_server.hpp"
#include "logger.hpp"
#include "options.hpp"
#include "udsf_config.hpp"
#include "HttpReq.hpp"


using namespace config;
udsf_config udsf_cfg;

int main(int argc, char **argv) {
    if (!Options::parse(argc, argv)) {
        std::cout << "Options::parse() failed" << std::endl;
        return 1;
    }

    Logger::init("UDSF", Options::getlogStdout(), Options::getlogRotFilelog());
    Logger::udsf_app().startup("Options parsed!");
    if(udsf_cfg.load(Options::getlibconfigConfig()) < 0) return 0;
         udsf_cfg.display();

    Logger::udsf_app().debug("Initiating UDSF server endpoints");
    Pistache::Address addr(udsf_cfg.sbi.addr4, Pistache::Port(udsf_cfg.sbi.port));
    MYSQL mysql;
    mysql_init(&mysql);
    if (!mysql_real_connect(&mysql, udsf_cfg.mysql.mysql_server.c_str(),
                          udsf_cfg.mysql.mysql_user.c_str(),
                          udsf_cfg.mysql.mysql_pass.c_str(),
                          udsf_cfg.mysql.mysql_db.c_str(), udsf_cfg.mysql.mysql_port, 0, 0)) {
        Logger::udsf_app().error("An error occurred while connecting to db: %s",
        mysql_error(&mysql));
        return 0;
    }
    UDSFApiServer udsfApiServer(addr, &mysql);
    udsfApiServer.init(PISTACHE_SERVER_THREADS);
    std::thread udsf_api_manager(&UDSFApiServer::start, udsfApiServer);

// while(1){
//     sleep(3);
//     HttpRequest* Http;
//     char http_return[4096] = {0};
//     char http_msg[4096] = {0};
//     strcpy(http_msg, "http://192.168.28.128:8081/nudsf-dr/v1/123_789/ue_context/records/223344");
//     nlohmann::json jjj;
//     jjj["meta"] ["tags"] = {{"record_id",nlohmann::json::array({"223344"})},{"from_nf_ID",nlohmann::json::array({"AMF_1234"})}} ;
//     jjj["blocks"] = nlohmann::json::array({
//                                             nlohmann::json::object({
//                                                 {"imsi","012345"},
//                                                 {"imei","gNB-555"},
//                                                 {"_KEY","46011"},
//                                                 {"OPC","connect"},
//                                                 {"keliang","connect"}
//                                                 })
//                                           });
//     char body[4096] =  {0};
//     strcpy(body,jjj.dump().data());
//     if(Http->HttpPut(http_msg, body,http_return)){
//         Logger::udsf_server().info(" http response:%s",http_return); 
//     }
// }


    pause();
    mysql_close(&mysql);
    return 0;
}

