/* ****************************************************************************
*
* postEntities - 
*
* POST /v2/entities
*
* Payload In:  Entity
* Payload Out: None
*
* URI parameters:
*   - 
*
* 01. Fill in UpdateContextRequest
* 02. Call standard op postUpdateContext
* 03. Prepare HTTP headers
* 04. Cleanup and return result
*/
std::string postEntities
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  Entity*  eP = &parseDataP->ent.res;

  if (!legalEntityLength(eP, ciP->servicePath))
  {
    OrionError oe(SccBadRequest, "Too long entity id/type/servicePath combination");
    ciP->httpStatusCode = SccBadRequest;
    eP->release();
    return oe.render(ciP, "");
  }

  // 01. Fill in UpdateContextRequest
  parseDataP->upcr.res.fill(eP, "APPEND_STRICT");
  

  // 02. Call standard op postUpdateContext
  postUpdateContext(ciP, components, compV, parseDataP, true);

  HttpStatusCode rcode = parseDataP->upcrs.res.contextElementResponseVector[0]->statusCode.code;

  std::string answer;

  // 03. Prepare HTTP headers
  if (rcode == SccOk || rcode == SccNone)
  {
    std::string location = "/v2/entities/" + eP->id;

    ciP->httpHeader.push_back("Location");
    ciP->httpHeaderValue.push_back(location);
    ciP->httpStatusCode = SccCreated;
  }
  else if (rcode == SccInvalidModification)
  {
    OrionError oe(SccInvalidModification, "Entity already exists");
    ciP->httpStatusCode = SccInvalidModification;
    answer = oe.render(ciP, "");
  }


  // 04. Cleanup and return result
  eP->release();

  return answer;
}
/* ****************************************************************************
*
* postEntities - 
*
* POST /v2/entities
*
* Payload In:  Entity
* Payload Out: None
*
* URI parameters:
*   options=keyValues
*   options=upsert
*
* 01. Fill in UpdateContextRequest
* 02. Call standard op postUpdateContext
* 03. Prepare HTTP headers
* 04. Cleanup and return result
*/
std::string postEntities
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  Entity*   eP = &parseDataP->ent.res;
  bool  upsert = ciP->uriParamOptions[OPT_UPSERT];

  if (!legalEntityLength(eP, ciP->httpHeaders.servicePath))
  {
    OrionError oe(SccBadRequest, "Too long entity id/type/servicePath combination", "BadRequest");
    eP->release();

    std::string out;
    TIMED_RENDER(out = oe.toJson());
    ciP->httpStatusCode = oe.code;

    return out;
  }

  // Set some aspects depending on upsert or not upsert
  ActionType      actionType;
  Ngsiv2Flavour   ngsiv2flavour;
  HttpStatusCode  sccCodeOnSuccess;
  if (upsert)
  {
    actionType       = ActionTypeAppend;
    ngsiv2flavour    = NGSIV2_NO_FLAVOUR;
    sccCodeOnSuccess = SccNoContent;
  }
  else
  {
    actionType       = ActionTypeAppendStrict;
    ngsiv2flavour    = NGSIV2_FLAVOUR_ONCREATE;
    sccCodeOnSuccess = SccCreated;
  }

  // 01. Fill in UpdateContextRequest
  parseDataP->upcr.res.fill(eP, actionType);

  // 02. Call standard op postUpdateContext
  postUpdateContext(ciP, components, compV, parseDataP, ngsiv2flavour);

  //
  // 03. Check error - 3 different ways to get an error from postUpdateContext ... :-(
  //     FIXME P4: make postUpdateContext have ONE way to return errors. See github issue #2763
  //
  std::string  answer = "";
  if (parseDataP->upcrs.res.oe.code != SccNone)
  {
    TIMED_RENDER(answer = parseDataP->upcrs.res.oe.toJson());
    ciP->httpStatusCode = parseDataP->upcrs.res.oe.code;
  }
  else if (parseDataP->upcrs.res.errorCode.code != SccOk)
  {
    ciP->httpStatusCode = parseDataP->upcrs.res.errorCode.code;
    TIMED_RENDER(answer = parseDataP->upcrs.res.errorCode.toJson());
    ciP->answer         = answer;
  }
  else
  {
    // Prepare HTTP headers
    std::string location = "/v2/entities/" + eP->id;
    if (eP->type != "" )
    {
      location += "?type=" + eP->type;
    }
    else
    {
      location += "?type=none";
    }

    ciP->httpHeader.push_back(HTTP_RESOURCE_LOCATION);
    ciP->httpHeaderValue.push_back(location);
    ciP->httpStatusCode = sccCodeOnSuccess;
  }

  // 04. Cleanup and return result
  eP->release();

  return answer;
}