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

import com.kamco.cd.training.train.dto.ModelTrainJobDto;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.ZonedDateTime;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

@Getter
@Setter
@Entity
@Table(name = "tb_model_train_job")
public class ModelTrainJobEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id", nullable = false)
  private Long id;

  @NotNull
  @Column(name = "model_id", nullable = false)
  private Long modelId;

  @NotNull
  @Column(name = "attempt_no", nullable = false)
  private Integer attemptNo;

  @Size(max = 30)
  @NotNull
  @Column(name = "status_cd", nullable = false, length = 30)
  private String statusCd;

  @NotNull
  @Column(name = "params_json", nullable = false)
  @JdbcTypeCode(SqlTypes.JSON)
  private Map<String, Object> paramsJson;

  @Size(max = 200)
  @Column(name = "container_name", length = 200)
  private String containerName;

  @Size(max = 500)
  @Column(name = "log_path", length = 500)
  private String logPath;

  @Column(name = "exit_code")
  private Integer exitCode;

  @Column(name = "error_message", columnDefinition = "TEXT")
  private String errorMessage;

  @ColumnDefault("now()")
  @Column(name = "queued_dttm")
  private ZonedDateTime queuedDttm;

  @Column(name = "started_dttm")
  private ZonedDateTime startedDttm;

  @Column(name = "finished_dttm")
  private ZonedDateTime finishedDttm;

  @Column(name = "locked_dttm")
  private ZonedDateTime lockedDttm;

  @Size(max = 100)
  @Column(name = "locked_by", length = 100)
  private String lockedBy;

  @Column(name = "total_epoch")
  private Integer totalEpoch;

  @Column(name = "current_epoch")
  private Integer currentEpoch;

  @Column(name = "job_type")
  private String jobType;

  public ModelTrainJobDto toDto() {
    return new ModelTrainJobDto(
        this.id,
        this.modelId,
        this.attemptNo,
        this.statusCd,
        this.exitCode,
        this.errorMessage,
        this.containerName,
        this.paramsJson,
        this.queuedDttm,
        this.startedDttm,
        this.finishedDttm,
        this.totalEpoch,
        this.currentEpoch);
  }
}
