package com.kamco.cd.training.dataset.dto;

import com.fasterxml.jackson.databind.JsonNode;
import com.kamco.cd.training.common.enums.DetectionClassification;
import com.kamco.cd.training.common.utils.HeaderUtil;
import com.kamco.cd.training.common.utils.enums.CodeExpose;
import com.kamco.cd.training.common.utils.enums.EnumType;
import com.kamco.cd.training.common.utils.interfaces.JsonFormatDttm;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.ZonedDateTime;
import java.util.UUID;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

@Slf4j
public class DatasetClsObjDto {

  @Schema(name = "DatasetClsObj Basic", description = "분류 데이터셋 객체 Obj 기본 정보")
  @Getter
  @Setter
  @NoArgsConstructor
  public static class Basic {

    private Long objId;
    private UUID uuid;
    private Long datasetUid;
    private String type;
    private Integer yyyy;
    private String classCd;
    private String mapSheetNum;
    private String tifImgPath;
    private String fileName;
    @JsonFormatDttm private ZonedDateTime createdDttm;
    private Long createdUid;
    private Boolean deleted;

    public Basic(
        Long objId,
        UUID uuid,
        Long datasetUid,
        String type,
        Integer yyyy,
        String classCd,
        String mapSheetNum,
        String tifImgPath,
        String fileName,
        ZonedDateTime createdDttm,
        Long createdUid,
        Boolean deleted) {
      this.objId = objId;
      this.uuid = uuid;
      this.datasetUid = datasetUid;
      this.type = type;
      this.yyyy = yyyy;
      this.classCd = classCd;
      this.mapSheetNum = mapSheetNum;
      this.tifImgPath = tifImgPath;
      this.fileName = fileName;
      this.createdDttm = createdDttm;
      this.createdUid = createdUid;
      this.deleted = deleted;
    }
  }

  @Schema(name = "ClsSearchReq", description = "분류 데이터셋 상세 도엽목록 조회 요청")
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class ClsSearchReq {
    @Schema(description = "회차 uuid", example = "e9a6774b-4f81-4402-b080-51d27fac1f01")
    private UUID uuid;

    @Schema(description = "년도", example = "2021")
    private Integer yyyy;

    @Schema(description = "분류", example = "waste")
    private String classCd;

    @Schema(description = "도엽번호", example = "36713060")
    private String mapSheetNum;

    @Schema(description = "페이지 번호 (0부터 시작)", example = "0")
    private int page = 0;

    @Schema(description = "페이지 크기", example = "20")
    private int size = 20;

    public Pageable toPageable() {
      return PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdDttm"));
    }
  }

  @Getter
  @Setter
  @AllArgsConstructor
  @NoArgsConstructor
  public static class DatasetClass {
    private String classCd;

    public String getClassName() {

      return HeaderUtil.isEnglishRequest()
          ? DetectionClassification.fromString(classCd).getId()
          : DetectionClassification.fromString(classCd).getDesc();
      // fromString 메서드를 사용하여 안전하게 변환 (미정의 값은 ETC로 처리)
      // return DetectionClassification.valueOf(classCd.toUpperCase()).getDesc();
    }
  }

  @Getter
  @Setter
  public static class DatasetStorage {
    private String usableBytes;
  }

  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  @Builder
  public static class DatasetObjRegDto {
    private Long datasetUid;
    private Integer compareYyyy;
    private String compareClassCd;
    private Integer targetYyyy;
    private String targetClassCd;
    private String comparePath;
    private String targetPath;
    private String labelPath;
    private String geojsonPath;
    private String mapSheetNum;
    private JsonNode geojson;
    private String fileName;
  }

  @CodeExpose
  @Getter
  @AllArgsConstructor
  public enum FolderType implements EnumType {
    TRAIN("train"),
    VAL("val"),
    TEST("test");

    private String desc;

    @Override
    public String getId() {
      return name();
    }

    @Override
    public String getText() {
      return desc;
    }
  }
}
