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

import com.kamco.cd.training.common.utils.enums.CodeExpose;
import com.kamco.cd.training.common.utils.enums.EnumType;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDate;
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 ErrorLogDto {

  @Schema(name = "ErrorLogBasic", description = "에러로그 기본 정보")
  @Getter
  @Setter
  @AllArgsConstructor
  public static class Basic {

    private final Long id;
    private final String serviceName;
    private final String menuNm;
    private final String loginId;
    private final String userName;
    private final EventType errorType;
    private final String errorName;
    private final LogErrorLevel errorLevel;
    private final String errorCode;
    private final String errorMessage;
    private final String errorDetail;
    private final String createDate; // to_char해서 가져옴
  }

  @Schema(name = "ErrorSearchReq", description = "에러로그 검색 요청")
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class ErrorSearchReq {

    LogErrorLevel errorLevel;
    EventType eventType;
    LocalDate startDate;
    LocalDate endDate;

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

    public ErrorSearchReq(
        LogErrorLevel errorLevel,
        EventType eventType,
        LocalDate startDate,
        LocalDate endDate,
        int page,
        int size) {
      this.errorLevel = errorLevel;
      this.eventType = eventType;
      this.startDate = startDate;
      this.endDate = endDate;
      this.page = page;
      this.size = size;
    }

    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);
    }
  }

  @CodeExpose
  public enum LogErrorLevel implements EnumType {
    WARNING("Warning"),
    ERROR("Error"),
    CRITICAL("Critical");

    private final String desc;

    LogErrorLevel(String desc) {
      this.desc = desc;
    }

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

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