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

import com.kamco.cd.training.dataset.dto.DatasetDto;
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.Instant;
import java.time.ZonedDateTime;
import java.util.Map;
import java.util.UUID;
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_dataset")
public class DatasetEntity {

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

  @Column(name = "compare_yyyy", nullable = false)
  private Integer compareYyyy;

  @Column(name = "target_yyyy", nullable = false)
  private Integer targetYyyy;

  @Size(max = 50)
  @ColumnDefault("'PRODUCTION'")
  @Column(name = "data_type", length = 50)
  private String dataType;

  @ColumnDefault("1")
  @Column(name = "round_no")
  private Long roundNo;

  @ColumnDefault("0")
  @Column(name = "total_size")
  private Long totalSize;

  @ColumnDefault("0")
  @Column(name = "item_count")
  private Long itemCount;

  @Column(name = "memo", length = Integer.MAX_VALUE)
  private String memo;

  @Size(max = 20)
  @ColumnDefault("'READY'")
  @Column(name = "status", length = 20)
  private String status;

  @Size(max = 50)
  @Column(name = "reg_user_id", length = 50)
  private String regUserId;

  @ColumnDefault("now()")
  @Column(name = "reg_dttm")
  private Instant regDttm;

  @Column(name = "mod_dttm")
  private Instant modDttm;

  @NotNull
  @Column(name = "id", nullable = false)
  private Long LegacyId;

  @Size(max = 50)
  @Column(name = "created_by", length = 50)
  private String createdBy;

  @NotNull
  @ColumnDefault("now()")
  @Column(name = "created_dttm", nullable = false)
  private ZonedDateTime createdDttm = ZonedDateTime.now();

  @NotNull
  @ColumnDefault("false")
  @Column(name = "deleted", nullable = false)
  private Boolean deleted = false;

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

  @Column(name = "total_items")
  private Long totalItems;

  @Size(max = 50)
  @Column(name = "updated_by", length = 50)
  private String updatedBy;

  @Column(name = "updated_dttm")
  private ZonedDateTime updatedDttm;

  @NotNull
  @ColumnDefault("uuid_generate_v4()")
  @Column(name = "uuid", nullable = false)
  private UUID uuid = UUID.randomUUID();

  @ColumnDefault("0")
  @Column(name = "total_object_count")
  private Long totalObjectCount;

  @Size(max = 1000)
  @Column(name = "dataset_path", length = 1000)
  private String datasetPath;

  // TODO 미사용시작
  @Column(name = "class_counts")
  @JdbcTypeCode(SqlTypes.JSON)
  private Map<String, Integer> classCounts;

  // TODO 미사용 끝
  @Size(max = 32)
  @Column(name = "uid")
  private String uid;

  @Column(name = "cd_cls_type")
  private String cdClsType;

  public DatasetDto.Basic toDto() {
    return new DatasetDto.Basic(
        this.id,
        this.uuid,
        this.title,
        this.roundNo,
        this.compareYyyy,
        this.targetYyyy,
        this.totalSize,
        this.memo,
        this.createdDttm,
        this.status,
        this.deleted,
        this.dataType,
        this.cdClsType);
  }
}
