package io.netfoundry.reporting.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import java.sql.Timestamp;
import java.util.UUID;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Data
public class BaseDTO {

  /**
   * The internal UUID that uniquely identifies this entity.
   *
   * NOTE: as of upgrade to Spring Boot 2.X and Spring Hateoas 1.1.X the below no longer seems to be
   * the case but the naming convention is left intact because it would break everything downstream.
   *
   * Implementation Note: Our entities are often wrapped by a
   * {@link org.springframework.hateoas.EntityModel} which uses a Json {@link JsonUnwrapped} directive
   * to "pull up" these properties to make them appear as if they are properties on the
   * {@link org.springframework.hateoas.EntityModel} instance itself. This is normally fine, but the
   * {@link org.springframework.hateoas.EntityModel} class extends {@link ResourceSupport} which
   * defines an implicit "id" property via its {@link ResourceSupport#getId()} method AND annotates it
   * with {@link JsonIgnore}. As a result, it is not possible to deserialize a Resource<? extends
   * BaseDTO> and retain the local id property, since Jackson first sees the ignore annotation and
   * presumes it should ignore the "id" property in the JSON. To get around this, we change the
   * JSON-level property name to "nfId" both in the tx side (BaseEntity in data project) and here.
   * This name change only applies at the serialized level.
   */
  private UUID id;
  private Timestamp createdAt;
  private Timestamp updatedAt;
  private String name;

  @JsonProperty("nfId")
  public void setId(UUID id) {
    this.id = id;
  }

  public BaseDTO(BaseDTO base) {
    this.id = base.getId();
    this.createdAt = base.getCreatedAt();
    this.updatedAt = base.getUpdatedAt();
    this.name = base.getName();
  }

}
