/**
 * Generated by API Builder - https://www.apibuilder.io
 * Service version: 0.0.0-beta.1
 * User agent: apibuilder app.apibuilder.io/chio/core-common/0.0.0-beta.1/http4s_0_17
 */
package io.chio.core.common.v0.models {

  /**
   * A generic error providing a message and some basic meta data.
   *
   * @param id A guid for identifying the error e.g. in log files.
   * @param timestamp Timestamp of the when the error occurred.
   * @param source An optional source on which the error occurred.
   * @param `type` An optional type for categorizing e.g. `validation` or `invalid_state`.
   * @param message The actual message of the error.
   */

  final case class GenericError(
    id: _root_.java.util.UUID,
    timestamp: _root_.java.time.Instant,
    source: _root_.scala.Option[String] = None,
    `type`: _root_.scala.Option[String] = None,
    message: String
  )

  /**
   * Response model indicating the health of a service.
   *
   * @param health The current health of the service. Normally this is `healthy`, otherwise an
   *        simple error message can be shown.
   */

  final case class Healthcheck(
    health: String
  )

}

package io.chio.core.common.v0.models {

  package object json {
    import io.circe.Decoder._
    import io.circe.Encoder._
    import scala.language.implicitConversions // See below - Make Scala 2.11 Either monadic
    import scala.util.Try
    import io.circe.{Json, JsonObject, Encoder, Decoder, DecodingFailure}
    import io.circe.syntax._
    import io.chio.core.common.v0.models.json._

    // Make Scala 2.11 Either monadic
    private[v0] implicit def eitherOps[A,B](e: Either[A,B]) = cats.implicits.catsSyntaxEither(e)

    private[v0] implicit val decodeUUID: Decoder[_root_.java.util.UUID] =
      Decoder.decodeString.emapTry(str => Try(_root_.java.util.UUID.fromString(str)))

    private[v0] implicit val encodeUUID: Encoder[_root_.java.util.UUID] =
      Encoder.encodeString.contramap[_root_.java.util.UUID](uuid => uuid.toString)

    private[v0] implicit val decodeInstant: Decoder[_root_.java.time.Instant] =
      Decoder.decodeString.emapTry(str => Try(_root_.java.time.OffsetDateTime.parse(str).toInstant))

    private[v0] implicit val encodeInstant: Encoder[_root_.java.time.Instant] =
      Encoder.encodeString.contramap[_root_.java.time.Instant](_.toString)

    private[v0] implicit val decodeLocalDate: Decoder[_root_.java.time.LocalDate] =
      Decoder.decodeString.emapTry(str => Try(_root_.java.time.LocalDate.parse(str)))

    private[v0] implicit val encodeLocalDate: Encoder[_root_.java.time.LocalDate] =
      Encoder.encodeString.contramap[_root_.java.time.LocalDate](_.toString)

    implicit def decodeCoreCommonGenericError: Decoder[GenericError] = Decoder.instance { c =>
     for {
        id <- c.downField("id").as[_root_.java.util.UUID]
        timestamp <- c.downField("timestamp").as[_root_.java.time.Instant]
        source <- c.downField("source").as[Option[String]]
        __type__ <- c.downField("type").as[Option[String]]
        message <- c.downField("message").as[String]
      } yield {
        GenericError(
          id = id,
          timestamp = timestamp,
          source = source,
          `type` = __type__,
          message = message
        )
      }
    }

    implicit def encodeCoreCommonGenericError: Encoder[GenericError] = Encoder.instance { t =>
      Json.fromFields(Seq(
        Some("id" -> t.id.asJson),
        Some("timestamp" -> t.timestamp.asJson),
        t.source.map(t => "source" -> t.asJson),
        t.`type`.map(t => "type" -> t.asJson),
        Some("message" -> t.message.asJson)
      ).flatten)
    }

    implicit def decodeCoreCommonHealthcheck: Decoder[Healthcheck] = Decoder.instance { c =>
     for {
        health <- c.downField("health").as[String]
      } yield {
        Healthcheck(
          health = health
        )
      }
    }

    implicit def encodeCoreCommonHealthcheck: Encoder[Healthcheck] = Encoder.instance { t =>
      Json.fromFields(Seq(
        Some("health" -> t.health.asJson)
      ).flatten)
    }
  }
}