package com.kamco.cd.kamcoback.scene.dto;

import com.fasterxml.jackson.databind.JsonNode;
import com.kamco.cd.kamcoback.common.enums.CommonUseStatus;
import com.kamco.cd.kamcoback.common.utils.enums.CodeExpose;
import com.kamco.cd.kamcoback.common.utils.enums.EnumType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.EntityNotFoundException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class MapInkxMngDto {

  @CodeExpose
  @Getter
  @AllArgsConstructor
  public enum UseInferenceType implements EnumType {
    USE("사용중"),
    EXCEPT("영구 추론제외");

    private final String desc;

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

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

  @Schema(name = "Basic", description = "Basic")
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class Basic {

    private Integer fid;
    private String mapidcdNo;
    private String mapidNm;
    private JsonNode geom;
    private String useInference;
    private ZonedDateTime createdDttm;
    private ZonedDateTime updatedDttm;
  }

  @Schema(name = "MapList", description = "목록 항목")
  @Getter
  @Setter
  @NoArgsConstructor
  public static class MapList {

    private Integer rowNum;
    private String mapidcdNo5k;
    private String mapidcdNo50k;
    private String mapidNm;
    private String createdDttm;
    private String updatedDttm;
    private String useInference;
    private ZonedDateTime createdDttmTime;
    private ZonedDateTime updatedDttmTime;

    // 목록 Querydsl 에서 리턴 받는 건 생성자 기준임 -> 쿼리 컬럼 그대로 받고 여기서 Java 형변환 해서 return 하기
    public MapList(
        Integer rowNum,
        String mapidcdNo5k,
        String mapidcdNo50k,
        String mapidNm,
        ZonedDateTime createdDttmTime,
        ZonedDateTime updatedDttmTime,
        CommonUseStatus useInference) {
      this.rowNum = rowNum;
      this.mapidcdNo5k = mapidcdNo5k;
      this.mapidcdNo50k = mapidcdNo50k;
      this.mapidNm = mapidNm;

      DateTimeFormatter fmt =
          DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.of("Asia/Seoul"));
      this.createdDttm = fmt.format(createdDttmTime);
      this.updatedDttm = fmt.format(updatedDttmTime);
      this.createdDttmTime = createdDttmTime;
      this.updatedDttmTime = updatedDttmTime;
      this.useInference = useInference.getId();
    }
  }

  @Schema(name = "searchReq", description = "검색 요청")
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class searchReq {

    // 페이징 파라미터
    private int page = 0;
    private int size = 20;
    private String sort;

    public Pageable toPageable() {
      if (sort != null && !sort.isEmpty()) {
        String[] sortParams = sort.split(",");
        String property = sortParams[0];
        Sort.Direction direction =
            sortParams.length > 1 ? Sort.Direction.fromString(sortParams[1]) : Sort.Direction.ASC;
        return PageRequest.of(page, size, Sort.by(direction, property));
      }
      return PageRequest.of(page, size);
    }
  }

  @Schema(name = "AddMapReq", description = "등록 요청")
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class AddMapReq {

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

    @Schema(description = "도엽명", example = "공덕")
    private String mapidNm;

    @Schema(
        description = "좌표 목록 (한 줄에 한 점, '경도 위도' 형식)",
        example =
            "127.17500001632317 36.17499998262991\n"
                + "127.14999995475043 36.17500002877932\n"
                + "127.15000004313612 36.199999984012415\n"
                + "127.1750000466954 36.20000001863179")
    private String coordinates;
  }

  @Schema(name = "UseInferReq", description = "추론제외 업데이트 요청")
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class UseInferReq {

    private String mapidcdNo;
    private CommonUseStatus useInference; // 변경하고자하는 상태

    public void valid() {
      if (mapidcdNo == null || mapidcdNo.isEmpty()) {
        throw new IllegalArgumentException("도엽번호는 필수 입력값입니다.");
      }
      // 공백제거
      mapidcdNo = mapidcdNo.trim();

      if (!mapidcdNo.matches("^\\d{8}$")) {
        throw new EntityNotFoundException("도엽번호는 8자리 숫자로 구성되어야 합니다.");
      }
    }
  }
}
