package com.kamco.cd.kamcoback.postgres.entity;

import com.kamco.cd.kamcoback.model.dto.ModelMngDto;
import com.kamco.cd.kamcoback.model.dto.ModelVerDto;
import com.kamco.cd.kamcoback.postgres.CommonDateEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.Map;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

@Getter
@Setter
@Entity
@Table(name = "tb_model_ver")
@NoArgsConstructor
public class ModelVerEntity extends CommonDateEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tb_model_ver_id_gen")
  @SequenceGenerator(
      name = "tb_model_ver_id_gen",
      sequenceName = "tb_model_ver_model_ver_uid",
      allocationSize = 1)
  @Column(name = "model_ver_uid", nullable = false)
  private Long id;

  @NotNull
  @Column(name = "model_uid", nullable = false)
  private Long modelUid;

  @Size(max = 64)
  @Column(name = "model_cate", length = 64)
  private String modelCate;

  @Size(max = 64)
  @Column(name = "model_ver", length = 64)
  private String modelVer;

  @Size(max = 20)
  @Column(name = "used_state", length = 20)
  private String usedState;

  @Size(max = 20)
  @Column(name = "model_state", length = 20)
  private String modelState;

  @Column(name = "quality_prob")
  private Double qualityProb;

  @Size(max = 20)
  @Column(name = "deploy_state", length = 20)
  private String deployState;

  @Size(max = 255)
  @Column(name = "model_path")
  private String modelPath;

  @Column(name = "created_uid")
  private Long createdUid;

  @Column(name = "updated_uid")
  private Long updatedUid;

  @Column(name = "deleted")
  private Boolean deleted = false;

  @Column(name = "leaning_rate", columnDefinition = "float8[]")
  private Double[] leaningRate;

  @Column(name = "batch_size", columnDefinition = "float8[]")
  private Double[] batchSize;

  @Column(name = "dropout_rate", columnDefinition = "float8[]")
  private Double[] dropoutRate;

  @Column(name = "number_of_filters", columnDefinition = "float8[]")
  private Double[] numberOfFilters;

  @Column(name = "best_state")
  private String bestState;

  @Column(name = "copy_state")
  private String copyState;

  @Column(name = "hyper_params")
  @JdbcTypeCode(SqlTypes.JSON)
  private Map<String, Object> hyperParams;

  @Column(name = "ver_cntnt", columnDefinition = "TEXT")
  private String verCntnt;

  public ModelVerEntity(
      Long id,
      Long modelUid,
      String modelCate,
      String modelVer,
      String usedState,
      String modelState,
      Double qualityProb,
      String deployState,
      String modelPath,
      Long createdUid,
      Long updatedUid,
      Boolean deleted) {
    this.id = id;
    this.modelUid = modelUid;
    this.modelCate = modelCate;
    this.modelVer = modelVer;
    this.usedState = usedState;
    this.modelState = modelState;
    this.qualityProb = qualityProb;
    this.deployState = deployState;
    this.modelPath = modelPath;
    this.createdUid = createdUid;
    this.updatedUid = updatedUid;
    this.deleted = deleted;
  }

  public ModelVerEntity(
      Long modelUid,
      String modelCate,
      String modelVer,
      String usedState,
      String modelState,
      Double qualityProb,
      String deployState,
      String modelPath,
      Long createdUid,
      Long updatedUid) {
    this.modelUid = modelUid;
    this.modelCate = modelCate;
    this.modelVer = modelVer;
    this.usedState = usedState;
    this.modelState = modelState;
    this.qualityProb = qualityProb;
    this.deployState = deployState;
    this.modelPath = modelPath;
    this.createdUid = createdUid;
    this.updatedUid = updatedUid;
  }

  public ModelVerDto.Basic toDto() {
    return new ModelVerDto.Basic(
        this.id,
        this.modelUid,
        this.modelCate,
        this.modelVer,
        this.usedState,
        this.modelState,
        this.qualityProb,
        this.deployState,
        this.modelPath,
        super.getCreatedDate(),
        this.createdUid,
        super.getModifiedDate(),
        this.updatedUid);
  }

  public void update(ModelMngDto.AddReq addReq) {
    this.modelCate = addReq.getModelCate();
    this.modelVer = addReq.getModelVer();
    this.modelPath = addReq.getModelPath();
  }

  public Boolean isDeleted() {
    return deleted;
  }

  public void deleted() {
    this.deleted = true;
  }
}
