/*
 * Daytona
 * Daytona AI platform API Docs
 *
 * The version of the OpenAPI document: 1.0
 * Contact: support@daytona.com
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


package io.daytona.api.client.model;

import java.util.Objects;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.daytona.api.client.JSON;

/**
 * LogEntry
 */
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.21.0")
public class LogEntry {
  public static final String SERIALIZED_NAME_TIMESTAMP = "timestamp";
  @SerializedName(SERIALIZED_NAME_TIMESTAMP)
  @javax.annotation.Nonnull
  private String timestamp;

  public static final String SERIALIZED_NAME_BODY = "body";
  @SerializedName(SERIALIZED_NAME_BODY)
  @javax.annotation.Nonnull
  private String body;

  public static final String SERIALIZED_NAME_SEVERITY_TEXT = "severityText";
  @SerializedName(SERIALIZED_NAME_SEVERITY_TEXT)
  @javax.annotation.Nonnull
  private String severityText;

  public static final String SERIALIZED_NAME_SEVERITY_NUMBER = "severityNumber";
  @SerializedName(SERIALIZED_NAME_SEVERITY_NUMBER)
  @javax.annotation.Nullable
  private BigDecimal severityNumber;

  public static final String SERIALIZED_NAME_SERVICE_NAME = "serviceName";
  @SerializedName(SERIALIZED_NAME_SERVICE_NAME)
  @javax.annotation.Nonnull
  private String serviceName;

  public static final String SERIALIZED_NAME_RESOURCE_ATTRIBUTES = "resourceAttributes";
  @SerializedName(SERIALIZED_NAME_RESOURCE_ATTRIBUTES)
  @javax.annotation.Nonnull
  private Map<String, String> resourceAttributes = new HashMap<>();

  public static final String SERIALIZED_NAME_LOG_ATTRIBUTES = "logAttributes";
  @SerializedName(SERIALIZED_NAME_LOG_ATTRIBUTES)
  @javax.annotation.Nonnull
  private Map<String, String> logAttributes = new HashMap<>();

  public static final String SERIALIZED_NAME_TRACE_ID = "traceId";
  @SerializedName(SERIALIZED_NAME_TRACE_ID)
  @javax.annotation.Nullable
  private String traceId;

  public static final String SERIALIZED_NAME_SPAN_ID = "spanId";
  @SerializedName(SERIALIZED_NAME_SPAN_ID)
  @javax.annotation.Nullable
  private String spanId;

  public LogEntry() {
  }

  public LogEntry timestamp(@javax.annotation.Nonnull String timestamp) {
    this.timestamp = timestamp;
    return this;
  }

  /**
   * Timestamp of the log entry
   * @return timestamp
   */
  @javax.annotation.Nonnull
  public String getTimestamp() {
    return timestamp;
  }

  public void setTimestamp(@javax.annotation.Nonnull String timestamp) {
    this.timestamp = timestamp;
  }


  public LogEntry body(@javax.annotation.Nonnull String body) {
    this.body = body;
    return this;
  }

  /**
   * Log message body
   * @return body
   */
  @javax.annotation.Nonnull
  public String getBody() {
    return body;
  }

  public void setBody(@javax.annotation.Nonnull String body) {
    this.body = body;
  }


  public LogEntry severityText(@javax.annotation.Nonnull String severityText) {
    this.severityText = severityText;
    return this;
  }

  /**
   * Severity level text (e.g., INFO, WARN, ERROR)
   * @return severityText
   */
  @javax.annotation.Nonnull
  public String getSeverityText() {
    return severityText;
  }

  public void setSeverityText(@javax.annotation.Nonnull String severityText) {
    this.severityText = severityText;
  }


  public LogEntry severityNumber(@javax.annotation.Nullable BigDecimal severityNumber) {
    this.severityNumber = severityNumber;
    return this;
  }

  /**
   * Severity level number
   * @return severityNumber
   */
  @javax.annotation.Nullable
  public BigDecimal getSeverityNumber() {
    return severityNumber;
  }

  public void setSeverityNumber(@javax.annotation.Nullable BigDecimal severityNumber) {
    this.severityNumber = severityNumber;
  }


  public LogEntry serviceName(@javax.annotation.Nonnull String serviceName) {
    this.serviceName = serviceName;
    return this;
  }

  /**
   * Service name that generated the log
   * @return serviceName
   */
  @javax.annotation.Nonnull
  public String getServiceName() {
    return serviceName;
  }

  public void setServiceName(@javax.annotation.Nonnull String serviceName) {
    this.serviceName = serviceName;
  }


  public LogEntry resourceAttributes(@javax.annotation.Nonnull Map<String, String> resourceAttributes) {
    this.resourceAttributes = resourceAttributes;
    return this;
  }

  public LogEntry putResourceAttributesItem(String key, String resourceAttributesItem) {
    if (this.resourceAttributes == null) {
      this.resourceAttributes = new HashMap<>();
    }
    this.resourceAttributes.put(key, resourceAttributesItem);
    return this;
  }

  /**
   * Resource attributes from OTEL
   * @return resourceAttributes
   */
  @javax.annotation.Nonnull
  public Map<String, String> getResourceAttributes() {
    return resourceAttributes;
  }

  public void setResourceAttributes(@javax.annotation.Nonnull Map<String, String> resourceAttributes) {
    this.resourceAttributes = resourceAttributes;
  }


  public LogEntry logAttributes(@javax.annotation.Nonnull Map<String, String> logAttributes) {
    this.logAttributes = logAttributes;
    return this;
  }

  public LogEntry putLogAttributesItem(String key, String logAttributesItem) {
    if (this.logAttributes == null) {
      this.logAttributes = new HashMap<>();
    }
    this.logAttributes.put(key, logAttributesItem);
    return this;
  }

  /**
   * Log-specific attributes
   * @return logAttributes
   */
  @javax.annotation.Nonnull
  public Map<String, String> getLogAttributes() {
    return logAttributes;
  }

  public void setLogAttributes(@javax.annotation.Nonnull Map<String, String> logAttributes) {
    this.logAttributes = logAttributes;
  }


  public LogEntry traceId(@javax.annotation.Nullable String traceId) {
    this.traceId = traceId;
    return this;
  }

  /**
   * Associated trace ID if available
   * @return traceId
   */
  @javax.annotation.Nullable
  public String getTraceId() {
    return traceId;
  }

  public void setTraceId(@javax.annotation.Nullable String traceId) {
    this.traceId = traceId;
  }


  public LogEntry spanId(@javax.annotation.Nullable String spanId) {
    this.spanId = spanId;
    return this;
  }

  /**
   * Associated span ID if available
   * @return spanId
   */
  @javax.annotation.Nullable
  public String getSpanId() {
    return spanId;
  }

  public void setSpanId(@javax.annotation.Nullable String spanId) {
    this.spanId = spanId;
  }

  /**
   * A container for additional, undeclared properties.
   * This is a holder for any undeclared properties as specified with
   * the 'additionalProperties' keyword in the OAS document.
   */
  private Map<String, Object> additionalProperties;

  /**
   * Set the additional (undeclared) property with the specified name and value.
   * If the property does not already exist, create it otherwise replace it.
   *
   * @param key name of the property
   * @param value value of the property
   * @return the LogEntry instance itself
   */
  public LogEntry putAdditionalProperty(String key, Object value) {
    if (this.additionalProperties == null) {
        this.additionalProperties = new HashMap<String, Object>();
    }
    this.additionalProperties.put(key, value);
    return this;
  }

  /**
   * Return the additional (undeclared) property.
   *
   * @return a map of objects
   */
  public Map<String, Object> getAdditionalProperties() {
    return additionalProperties;
  }

  /**
   * Return the additional (undeclared) property with the specified name.
   *
   * @param key name of the property
   * @return an object
   */
  public Object getAdditionalProperty(String key) {
    if (this.additionalProperties == null) {
        return null;
    }
    return this.additionalProperties.get(key);
  }


  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    LogEntry logEntry = (LogEntry) o;
    return Objects.equals(this.timestamp, logEntry.timestamp) &&
        Objects.equals(this.body, logEntry.body) &&
        Objects.equals(this.severityText, logEntry.severityText) &&
        Objects.equals(this.severityNumber, logEntry.severityNumber) &&
        Objects.equals(this.serviceName, logEntry.serviceName) &&
        Objects.equals(this.resourceAttributes, logEntry.resourceAttributes) &&
        Objects.equals(this.logAttributes, logEntry.logAttributes) &&
        Objects.equals(this.traceId, logEntry.traceId) &&
        Objects.equals(this.spanId, logEntry.spanId)&&
        Objects.equals(this.additionalProperties, logEntry.additionalProperties);
  }

  @Override
  public int hashCode() {
    return Objects.hash(timestamp, body, severityText, severityNumber, serviceName, resourceAttributes, logAttributes, traceId, spanId, additionalProperties);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class LogEntry {\n");
    sb.append("    timestamp: ").append(toIndentedString(timestamp)).append("\n");
    sb.append("    body: ").append(toIndentedString(body)).append("\n");
    sb.append("    severityText: ").append(toIndentedString(severityText)).append("\n");
    sb.append("    severityNumber: ").append(toIndentedString(severityNumber)).append("\n");
    sb.append("    serviceName: ").append(toIndentedString(serviceName)).append("\n");
    sb.append("    resourceAttributes: ").append(toIndentedString(resourceAttributes)).append("\n");
    sb.append("    logAttributes: ").append(toIndentedString(logAttributes)).append("\n");
    sb.append("    traceId: ").append(toIndentedString(traceId)).append("\n");
    sb.append("    spanId: ").append(toIndentedString(spanId)).append("\n");
    sb.append("    additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(Object o) {
    return o == null ? "null" : o.toString().replace("\n", "\n    ");
  }


  public static HashSet<String> openapiFields;
  public static HashSet<String> openapiRequiredFields;

  static {
    // a set of all properties/fields (JSON key names)
    openapiFields = new HashSet<String>(Arrays.asList("timestamp", "body", "severityText", "severityNumber", "serviceName", "resourceAttributes", "logAttributes", "traceId", "spanId"));

    // a set of required properties/fields (JSON key names)
    openapiRequiredFields = new HashSet<String>(Arrays.asList("timestamp", "body", "severityText", "serviceName", "resourceAttributes", "logAttributes"));
  }

  /**
   * Validates the JSON Element and throws an exception if issues found
   *
   * @param jsonElement JSON Element
   * @throws IOException if the JSON Element is invalid with respect to LogEntry
   */
  public static void validateJsonElement(JsonElement jsonElement) throws IOException {
      if (jsonElement == null) {
        if (!LogEntry.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null
          throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "The required field(s) %s in LogEntry is not found in the empty JSON string", LogEntry.openapiRequiredFields.toString()));
        }
      }

      // check to make sure all required properties/fields are present in the JSON string
      for (String requiredField : LogEntry.openapiRequiredFields) {
        if (jsonElement.getAsJsonObject().get(requiredField) == null) {
          throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "The required field `%s` is not found in the JSON string: %s", requiredField, jsonElement.toString()));
        }
      }
        JsonObject jsonObj = jsonElement.getAsJsonObject();
      if (!jsonObj.get("timestamp").isJsonPrimitive()) {
        throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `timestamp` to be a primitive type in the JSON string but got `%s`", jsonObj.get("timestamp").toString()));
      }
      if (!jsonObj.get("body").isJsonPrimitive()) {
        throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `body` to be a primitive type in the JSON string but got `%s`", jsonObj.get("body").toString()));
      }
      if (!jsonObj.get("severityText").isJsonPrimitive()) {
        throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `severityText` to be a primitive type in the JSON string but got `%s`", jsonObj.get("severityText").toString()));
      }
      if (!jsonObj.get("serviceName").isJsonPrimitive()) {
        throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `serviceName` to be a primitive type in the JSON string but got `%s`", jsonObj.get("serviceName").toString()));
      }
      if ((jsonObj.get("traceId") != null && !jsonObj.get("traceId").isJsonNull()) && !jsonObj.get("traceId").isJsonPrimitive()) {
        throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `traceId` to be a primitive type in the JSON string but got `%s`", jsonObj.get("traceId").toString()));
      }
      if ((jsonObj.get("spanId") != null && !jsonObj.get("spanId").isJsonNull()) && !jsonObj.get("spanId").isJsonPrimitive()) {
        throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "Expected the field `spanId` to be a primitive type in the JSON string but got `%s`", jsonObj.get("spanId").toString()));
      }
  }

  public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
    @SuppressWarnings("unchecked")
    @Override
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
       if (!LogEntry.class.isAssignableFrom(type.getRawType())) {
         return null; // this class only serializes 'LogEntry' and its subtypes
       }
       final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
       final TypeAdapter<LogEntry> thisAdapter
                        = gson.getDelegateAdapter(this, TypeToken.get(LogEntry.class));

       return (TypeAdapter<T>) new TypeAdapter<LogEntry>() {
           @Override
           public void write(JsonWriter out, LogEntry value) throws IOException {
             JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject();
             obj.remove("additionalProperties");
             // serialize additional properties
             if (value.getAdditionalProperties() != null) {
               for (Map.Entry<String, Object> entry : value.getAdditionalProperties().entrySet()) {
                 if (entry.getValue() instanceof String)
                   obj.addProperty(entry.getKey(), (String) entry.getValue());
                 else if (entry.getValue() instanceof Number)
                   obj.addProperty(entry.getKey(), (Number) entry.getValue());
                 else if (entry.getValue() instanceof Boolean)
                   obj.addProperty(entry.getKey(), (Boolean) entry.getValue());
                 else if (entry.getValue() instanceof Character)
                   obj.addProperty(entry.getKey(), (Character) entry.getValue());
                 else {
                   JsonElement jsonElement = gson.toJsonTree(entry.getValue());
                   if (jsonElement.isJsonArray()) {
                     obj.add(entry.getKey(), jsonElement.getAsJsonArray());
                   } else {
                     obj.add(entry.getKey(), jsonElement.getAsJsonObject());
                   }
                 }
               }
             }
             elementAdapter.write(out, obj);
           }

           @Override
           public LogEntry read(JsonReader in) throws IOException {
             JsonElement jsonElement = elementAdapter.read(in);
             validateJsonElement(jsonElement);
             JsonObject jsonObj = jsonElement.getAsJsonObject();
             // store additional fields in the deserialized instance
             LogEntry instance = thisAdapter.fromJsonTree(jsonObj);
             for (Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) {
               if (!openapiFields.contains(entry.getKey())) {
                 if (entry.getValue().isJsonPrimitive()) { // primitive type
                   if (entry.getValue().getAsJsonPrimitive().isString())
                     instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsString());
                   else if (entry.getValue().getAsJsonPrimitive().isNumber())
                     instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsNumber());
                   else if (entry.getValue().getAsJsonPrimitive().isBoolean())
                     instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsBoolean());
                   else
                     throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "The field `%s` has unknown primitive type. Value: %s", entry.getKey(), entry.getValue().toString()));
                 } else if (entry.getValue().isJsonArray()) {
                     instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), List.class));
                 } else { // JSON object
                     instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), HashMap.class));
                 }
               }
             }
             return instance;
           }

       }.nullSafe();
    }
  }

  /**
   * Create an instance of LogEntry given an JSON string
   *
   * @param jsonString JSON string
   * @return An instance of LogEntry
   * @throws IOException if the JSON string is invalid with respect to LogEntry
   */
  public static LogEntry fromJson(String jsonString) throws IOException {
    return JSON.getGson().fromJson(jsonString, LogEntry.class);
  }

  /**
   * Convert an instance of LogEntry to an JSON string
   *
   * @return JSON string
   */
  public String toJson() {
    return JSON.getGson().toJson(this);
  }
}

