/**
 * Nudm_UEAU
 * UDM UE Authentication Service. � 2020, 3GPP Organizational Partners (ARIB,
 * ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
 *
 * The version of the OpenAPI document: 1.2.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 "DeleteAuthApiImpl.h"

using namespace config;
extern udm_config udm_cfg;

namespace oai {
namespace udm {
namespace api {

using namespace oai::udm::model;

DeleteAuthApiImpl::DeleteAuthApiImpl(
    std::shared_ptr<Pistache::Rest::Router> rtr)
    : DeleteAuthApi(rtr) {}

void DeleteAuthApiImpl::delete_auth(const std::string &supi,
                                    const std::string &authEventId,
                                    const AuthEvent &authEvent,
                                    Pistache::Http::ResponseWriter &response) {

  Logger::udm_ueau().info("\n\nEntering delete_auth()");

  std::string udr_ip =
      std::string(inet_ntoa(*((struct in_addr *)&udm_cfg.nudr.addr4)));
  std::string udr_port = std::to_string(udm_cfg.nudr.port);
  std::string remoteUri;
  std::string Method;
  std::string msgBody;
  std::string Response;
  std::string Location;

  nlohmann::json j_ProblemDetails;
  ProblemDetails m_ProblemDetails;

  // UDR GET interface ----- get user info--------------------
  remoteUri = udr_ip + ":" + udr_port + "/nudr-dr/v2/subscription-data/" +
              supi + "/authentication-data/authentication-subscription";
  Logger::udm_ueau().debug("GET Request:" + remoteUri);
  Method = "GET";

  Curl::curl_http_client(remoteUri, Method, "", Response);

  nlohmann::json response_data = {};
  try {
    response_data = nlohmann::json::parse(Response.c_str());
  } catch (nlohmann::json::exception &e) { // error handling
    Logger::udm_ueau().info("Could not get Json content from UDR response");

    m_ProblemDetails.setCause("USER_NOT_FOUND");
    m_ProblemDetails.setStatus(404);
    m_ProblemDetails.setDetail("User " + supi + " not found in Database");
    to_json(j_ProblemDetails, m_ProblemDetails);

    Logger::udm_ueau().error("User " + supi + " not found in Database");
    Logger::udm_ueau().info("Send 404 Not_Found response to AUSF");
    response.send(Pistache::Http::Code::Not_Found, j_ProblemDetails.dump());
    return;
  }

  if (!authEvent.isAuthRemovalInd()) {
    // error handling
    m_ProblemDetails.setStatus(400);
    m_ProblemDetails.setDetail("authRemovalInd should be true");
    to_json(j_ProblemDetails, m_ProblemDetails);

    Logger::udm_ueau().error("authRemovalInd should be true");
    Logger::udm_ueau().info("Send 400 Bad_Request response to AUSF");
    response.send(Pistache::Http::Code::Bad_Request, j_ProblemDetails.dump());
    return;
  }

  std::string hash_value = sha256(supi + authEvent.getServingNetworkName());
  // Logger::udm_ueau().debug("\n\nauthEventId=" +
  // hash_value.substr(0,hash_value.length()/2));
  Logger::udm_ueau().debug("authEventId=" + hash_value);

  if (!hash_value.compare(authEventId)) {
    // UDR DELETE interface ------- delete authentication
    // status------------------------------
    remoteUri = udr_ip + ":" + udr_port + "/nudr-dr/v2/subscription-data/" +
                supi + "/authentication-data/authentication-status";

    Logger::udm_ueau().debug("DELETE Request:" + remoteUri);
    Method = "DELETE";

    nlohmann::json j_authEvent;
    to_json(j_authEvent, authEvent);

    Curl::curl_http_client(remoteUri, Method, "", Response);

    Logger::udm_ueau().info("Send 204 No_Content response to AUSF");
    response.send(Pistache::Http::Code::No_Content, "");
  } else {
    // error handling
    // wrong autheventid
    m_ProblemDetails.setCause("DATA_NOT_FOUND");
    m_ProblemDetails.setStatus(404);
    m_ProblemDetails.setDetail("Wrong authEventId");
    to_json(j_ProblemDetails, m_ProblemDetails);

    Logger::udm_ueau().error("Wrong authEventId, should be = " + hash_value);
    Logger::udm_ueau().info("Send 404 Not_Found response to AUSF");
    response.send(Pistache::Http::Code::Not_Found, j_ProblemDetails.dump());
  }
}

} // namespace api
} // namespace udm
} // namespace oai
