/*
 * Licensed to Open Source Radio Access Network(OS-RAN) Alliance and OAI
 * Software Alliance under one or more contributor license agreements. The
 * initial OpenXG series projects are derided from OAI projects, the files from
 * OAI projects are all in compliance with OAI Public License, Version 1.1.
 * codes and files developed from OpenXG projects and from OS-RAN Alliance
 * are all under OS-RAN licenses; you may not use this file except in compliance
 * with the license.  You may get a copy of the license at:
 *	http://www.openxg.org.cn/?falu_69.html
 * For more information about OpenXG, please contact:
 * contact@openxg.org.cn
 */

#include "NFStatusNotifyApiImpl.h"

#include "logger.hpp"
#include "smf_msg.hpp"
#include "itti_msg_sbi.hpp"
#include "smf_config.hpp"
#include "3gpp_conversions.hpp"

extern smf::smf_config smf_cfg;

namespace oai {
namespace smf_server {
namespace api {

using namespace oai::smf_server::model;

NFStatusNotifyApiImpl::NFStatusNotifyApiImpl(
    std::shared_ptr<Pistache::Rest::Router> rtr, smf::smf_app* smf_app_inst,
    std::string address)
    : NFStatusNotifyApi(rtr), m_smf_app(smf_app_inst), m_address(address) {}

void NFStatusNotifyApiImpl::receive_nf_status_notification(
    const NotificationData& notificationData,
    Pistache::Http::ResponseWriter& response) {
  Logger::smf_api_server().info(
      "NFStatusNotifyApiImpl, received a NF status notification...");

  smf::data_notification_msg notification_msg = {};
  // convert from NotificationData to data_notification_msg
  xgpp_conv::data_notification_from_openapi(notificationData, notification_msg);

  // Handle the message in smf_app
  std::shared_ptr<itti_sbi_notification_data> itti_msg =
      std::make_shared<itti_sbi_notification_data>(TASK_SMF_SBI, TASK_SMF_APP);
  itti_msg->notification_msg = notification_msg;
  itti_msg->http_version     = 1;

  ProblemDetails problem_details = {};
  uint8_t http_code              = 0;

  if (m_smf_app->handle_nf_status_notification(
          itti_msg, problem_details, http_code)) {
    response.send(Pistache::Http::Code(204));
  } else {
    nlohmann::json json_data = {};
    to_json(json_data, problem_details);
    // content type
    response.headers().add<Pistache::Http::Header::ContentType>(
        Pistache::Http::Mime::MediaType("application/problem+json"));
    response.send(Pistache::Http::Code(http_code), json_data.dump().c_str());
  }
}

}  // namespace api
}  // namespace smf_server
}  // namespace oai
