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

import com.kamco.cd.kamcoback.postgres.CommonDateEntity;
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.Size;
import java.time.ZonedDateTime;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * This class represents the entity for managing the history of map sheets. It is mapped to the
 * database table "tb_map_sheet_mng_hst" and contains various properties related to the 1:5k map
 * sheet information, as well as metadata for file synchronization and management.
 *
 * <p>This entity: - Includes a primary key (hstUid) for unique identification. - Maintains
 * information associated with map sheets such as code, name, scale ratio, and paths. - Tracks
 * states, timestamps, and data synchronization details. - Maintains relationships with the
 * `MapInkx5kEntity` entity through a many-to-one association. - Provides functionality to update
 * file information and sizes (`tifSizeBytes`, `tfwSizeBytes`, and `totalSizeBytes`).
 *
 * <p>It extends the `CommonDateEntity` class to include common date management fields, such as
 * creation and modification timestamps.
 *
 * <p>The `@Getter` annotation generates getter methods for all fields, while the access to setters
 * is restricted to enforce controlled modifications. The entity uses `@NoArgsConstructor` with
 * `AccessLevel.PROTECTED` to restrict direct instantiation. The `updateFileInfos` method allows
 * dynamic updates of specific file information.
 *
 * <p>Fields include: - hstUid: Unique identifier for the history record. - mngYyyy: Year associated
 * with the management record. - mapInkx5kByCode: Reference to the related `MapInkx5kEntity` object.
 * - mapSheetNum: Map sheet number identifying specific map. - mapSheetName: Name of the map sheet.
 * - mapSheetCodeSrc: Source code of the map sheet. - scaleRatio: Scale ratio of the map. -
 * dataState: State/status of the map sheet data. - dataStateDttm: Timestamp of the data state. -
 * useInference: Indicator or metadata for inference usage. - useInferenceDttm: Timestamp for
 * inference-related use. - mapSheetPath: Path or location of the map sheet file. - refMapSheetNum:
 * Reference to a related map sheet number. - createdUid: User ID of the record creator. -
 * updatedUid: User ID of the last updater. - syncState and related fields: Fields to manage
 * synchronization states and processes. - tifSizeBytes, tfwSizeBytes, totalSizeBytes: Fields to
 * track file size details. - sync file name fields: Stores names of files relevant for
 * synchronization and verification.
 *
 * <p>This entity is essential for tracking and managing map sheet revisions, status, and usage in a
 * system leveraging 1:5k map data.
 */
@Getter
// entity의 접근제어를 위해 @setter를 사용 x
// @Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
// 영상관리이력
@Table(name = "tb_map_sheet_mng_hst")
public class MapSheetMngHstEntity extends CommonDateEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "hst_uid")
  private Long hstUid; // id

  @Column(name = "mng_yyyy")
  private Integer mngYyyy; // 년도

  // JPA 연관관계: MapInkx5k 참조 (PK 기반) 소속도엽번호 1:5k
  /*
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "map_sheet_code", referencedColumnName = "fid")
  private MapInkx5kEntity mapInkx5kByCode;
  */
  // TODO 1:5k 관련 정보 추후 제거 필요
  @Column(name = "map_sheet_num")
  private String mapSheetNum; // 도엽번호

  @Column(name = "map_sheet_name")
  private String mapSheetName;

  // TODO END

  // 도엽파일이 저장된 경로
  @Column(name = "map_sheet_code_src")
  private Integer mapSheetCodeSrc;

  // 도엽비율?
  @Column(name = "scale_ratio")
  private Integer scaleRatio;

  @Column(name = "data_state", length = 20)
  private String dataState;

  @Column(name = "data_state_dttm")
  private ZonedDateTime dataStateDttm;

  @Column(name = "use_inference")
  private String useInference;

  @Column(name = "use_inference_dttm")
  private ZonedDateTime useInferenceDttm;

  @Column(name = "map_sheet_path")
  private String mapSheetPath;

  @Column(name = "ref_map_sheet_num")
  private String refMapSheetNum;

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

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

  @Size(max = 20)
  @Column(name = "sync_state", length = 20)
  private String syncState;

  @Size(max = 20)
  @Column(name = "sync_check_state", length = 20)
  private String syncCheckState;

  @Column(name = "sync_strt_dttm")
  private ZonedDateTime syncStrtDttm;

  @Column(name = "sync_end_dttm")
  private ZonedDateTime syncEndDttm;

  @Column(name = "sync_check_strt_dttm")
  private ZonedDateTime syncCheckStrtDttm;

  @Column(name = "sync_check_end_dttm")
  private ZonedDateTime syncCheckEndDttm;

  @Column(name = "tif_size_bytes")
  private Long tifSizeBytes;

  @Column(name = "tfw_size_bytes")
  private Long tfwSizeBytes;

  @Column(name = "total_size_bytes")
  private Long totalSizeBytes;

  @Size(max = 100)
  @Column(name = "sync_tif_file_name", length = 100)
  private String syncTifFileName;

  @Size(max = 100)
  @Column(name = "sync_tfw_file_name", length = 100)
  private String syncTfwFileName;

  @Size(max = 100)
  @Column(name = "sync_check_tif_file_name", length = 100)
  private String syncCheckTifFileName;

  @Size(max = 100)
  @Column(name = "sync_check_tfw_file_name", length = 100)
  private String syncCheckTfwFileName;

  // 파일정보 업데이트
  public void updateFileInfos(Long tifSizeBytes, Long tfwSizeBytes) {
    tifSizeBytes = tifSizeBytes == null ? 0L : tifSizeBytes;
    tfwSizeBytes = tfwSizeBytes == null ? 0L : tfwSizeBytes;
    this.tifSizeBytes = tifSizeBytes;
    this.tfwSizeBytes = tfwSizeBytes;
    this.totalSizeBytes = tifSizeBytes + tfwSizeBytes;
  }
}
