package com.oguerreiro.resilient.domain;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.oguerreiro.resilient.security.custom.ResilientSecurityResourceConfig;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;

/**
 * Unit Converter
 */
@Entity
@Table(name = "unit_converter")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@ResilientSecurityResourceConfig()
public class UnitConverter extends AbstractResilientLongIdEntity<UnitConverter> {

  private static final long serialVersionUID = 1L;

  @NotNull
  @Column(name = "name", nullable = false)
  private String name;

  @NotNull
  @Column(name = "description", nullable = false)
  private String description;

  /**
   * The convertion formula to convert the fromUnit into the toUnit.
   */
  @NotNull
  @Column(name = "convertion_formula", nullable = false)
  private String convertionFormula;

  @ManyToOne(fetch = FetchType.LAZY)
  @JsonIgnoreProperties(value = { "unitType" }, allowSetters = true)
  private Unit fromUnit;

  @ManyToOne(fetch = FetchType.LAZY)
  @JsonIgnoreProperties(value = { "unitType" }, allowSetters = true)
  private Unit toUnit;

  public String getName() {
    return this.name;
  }

  public UnitConverter name(String name) {
    this.setName(name);
    return this;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getDescription() {
    return this.description;
  }

  public UnitConverter description(String description) {
    this.setDescription(description);
    return this;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getConvertionFormula() {
    return this.convertionFormula;
  }

  public UnitConverter convertionFormula(String convertionFormula) {
    this.setConvertionFormula(convertionFormula);
    return this;
  }

  public void setConvertionFormula(String convertionFormula) {
    this.convertionFormula = convertionFormula;
  }

  public Unit getFromUnit() {
    return this.fromUnit;
  }

  public void setFromUnit(Unit unit) {
    this.fromUnit = unit;
  }

  public UnitConverter fromUnit(Unit unit) {
    this.setFromUnit(unit);
    return this;
  }

  public Unit getToUnit() {
    return this.toUnit;
  }

  public void setToUnit(Unit unit) {
    this.toUnit = unit;
  }

  public UnitConverter toUnit(Unit unit) {
    this.setToUnit(unit);
    return this;
  }

//@formatter:off
  @Override
  public String toString() {
    return "UnitConverter{" + 
              "id=" + getId() + 
              ", name='" + getName() + "'" + 
              ", description='" + getDescription() + "'" + 
              ", convertionFormula='" + getConvertionFormula() + "'" + 
              ", version=" + getVersion() + 
           "}";
  }
//@formatter:on
}
