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

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.ZonedDateTime;
import java.util.UUID;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;

@Getter
@Setter
@Entity
@Table(name = "tb_upload_session")
public class UploadSessionEntity {

  @Id
  @Column(name = "upload_id", nullable = false, length = 100)
  private String uploadId;

  @Size(max = 255)
  @NotNull
  @Column(name = "file_name", nullable = false, length = 255)
  private String fileName;

  @NotNull
  @Column(name = "file_size", nullable = false)
  private Long fileSize;

  @Column(name = "dataset_id")
  private Long datasetId;

  @Column(name = "total_chunks")
  private Integer totalChunks;

  @Column(name = "uploaded_chunks")
  private Integer uploadedChunks = 0;

  @Size(max = 20)
  @NotNull
  @Column(name = "status", nullable = false, length = 20)
  private String status; // INIT, UPLOADING, PROCESSING, COMPLETED, FAILED

  @Size(max = 500)
  @Column(name = "temp_path", length = 500)
  private String tempPath;

  @Size(max = 500)
  @Column(name = "final_path", length = 500)
  private String finalPath;

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

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

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

  @Column(name = "completed_dttm")
  private ZonedDateTime completedDttm;

  @Size(max = 50)
  @Column(name = "upload_divi", length = 50)
  private String uploadDivi;

  @Size(max = 300)
  @Column(name = "file_hash", length = 300)
  private String fileHash;

  @Column(name = "chunk_total_index")
  private Integer chunkTotalIndex;

  @Column(name = "chunk_index")
  private Integer chunkIndex = 0;

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

  @PrePersist
  protected void onPersist() {
    if (this.createdDttm == null) {
      this.createdDttm = ZonedDateTime.now();
    }
    if (this.chunkIndex == null) {
      this.chunkIndex = 0;
    }
  }
}
