/**
 * Namf_Communication
 * AMF Communication Service © 2019, 3GPP Organizational Partners (ARIB, ATIS,
 * CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
 *
 * The version of the OpenAPI document: 1.1.0.alpha-1
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator
 * (https://openapi-generator.tech). https://openapi-generator.tech Do not edit
 * the class manually.
 */

#include "N1MessageNotifyApi.h"
#include "Helpers.h"
#include "mime_parser.hpp"
#include "logger.hpp"
#include "amf_config.hpp"
#include "mime_parser.hpp"

extern config::amf_config amf_cfg;

namespace oai {
namespace amf {
namespace api {

using namespace oai::amf::helpers;
using namespace oai::amf::model;

N1MessageNotifyApi::N1MessageNotifyApi(
    std::shared_ptr<Pistache::Rest::Router> rtr) {
  router = rtr;
}

void N1MessageNotifyApi::init() {
  setupRoutes();
}

void N1MessageNotifyApi::setupRoutes() {
  using namespace Pistache::Rest;

  Routes::Post(
      *router,
      base + amf_cfg.sbi_api_version +
          "/ue-contexts/:ueContextId/n1-message-notify",
      Routes::bind(&N1MessageNotifyApi::n1_message_notify_handler, this));

  // Default handler, called when a route is not found
  router->addCustomHandler(Routes::bind(
      &N1MessageNotifyApi::n1_message_notify_default_handler, this));
}

void N1MessageNotifyApi::n1_message_notify_handler(
    const Pistache::Rest::Request& request,
    Pistache::Http::ResponseWriter response) {
  // Getting the path params
  auto ueContextId = request.param(":ueContextId").as<std::string>();
  Logger::amf_server().debug(
      "Received a N1MessageNotification with ue_ctx_id %s",
      ueContextId.c_str());

  N1MessageNotification n1MessageNotification = {};

  // simple parser
  mime_parser sp = {};
  sp.parse(request.body());

  std::vector<mime_part> parts = {};
  sp.get_mime_parts(parts);
  uint8_t size = parts.size();
  Logger::amf_server().debug("Number of MIME parts %d", size);

  // 2 parts for Json data and N1
  if (size != 2) {
    response.send(Pistache::Http::Code::Bad_Request);
    Logger::amf_server().debug("Bad request: should have 2 MIME parts");
    return;
  }

  Logger::amf_server().debug(
      "Request body, part 1: \n%s", parts[0].body.c_str());
  Logger::amf_server().debug(
      "Request body, part 2: \n %s", parts[1].body.c_str());

  try {
    nlohmann::json::parse(parts[0].body.c_str()).get_to(n1MessageNotification);
    this->receive_n1_message_notification(
        ueContextId, n1MessageNotification, parts[1].body, response);
  } catch (nlohmann::detail::exception& e) {
    // send a 400 error
    response.send(Pistache::Http::Code::Bad_Request, e.what());
    return;
  } catch (std::exception& e) {
    // send a 500 error
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
}

void N1MessageNotifyApi::n1_message_notify_default_handler(
    const Pistache::Rest::Request&, Pistache::Http::ResponseWriter response) {
  response.send(
      Pistache::Http::Code::Not_Found, "The requested method does not exist");
}

}  // namespace api
}  // namespace amf
}  // namespace oai
