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

import com.kamco.cd.kamcoback.common.utils.interfaces.JsonFormatDttm;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import java.time.ZonedDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

@RequiredArgsConstructor
public class AuthDto {

  @Getter
  @Setter
  public static class Basic {

    private Long id;
    private String userAuth;
    private String userNm;
    private String userId;
    private String empId;
    private String userEmail;
    @JsonFormatDttm private ZonedDateTime createdDttm;

    public Basic(
        Long id,
        String userAuth,
        String userNm,
        String userId,
        String empId,
        String userEmail,
        ZonedDateTime createdDttm) {
      this.id = id;
      this.userAuth = userAuth;
      this.userNm = userNm;
      this.userId = userId;
      this.empId = empId;
      this.userEmail = userEmail;
      this.createdDttm = createdDttm;
    }
  }

  @Schema(name = "save request", description = "사용자 등록 정보")
  @Getter
  @Setter
  public static class SaveReq {

    @Schema(description = "구분", example = "관리자/라벨러/검수자 중 하나")
    @NotBlank
    private String userAuth;

    @NotBlank
    @Schema(description = "이름", example = "홍길동")
    private String userNm;

    @Schema(description = "ID", example = "gildong")
    @NotBlank
    private String userId;

    @Schema(description = "PW", example = "password")
    @NotBlank
    private String userPw;

    @Schema(description = "사번", example = "사번")
    @NotBlank
    private String empId;

    @Schema(description = "이메일", example = "gildong@naver.com")
    @NotBlank
    private String userEmail;

    public SaveReq(
        String userAuth,
        String userNm,
        String userId,
        String userPw,
        String empId,
        String userEmail) {
      this.userAuth = userAuth;
      this.userNm = userNm;
      this.userId = userId;
      this.userPw = userPw;
      this.empId = empId;
      this.userEmail = userEmail;
    }
  }

  @Schema(name = "update request", description = "사용자 수정 정보")
  @Getter
  @Setter
  public static class UpdateReq {

    @Schema(description = "id", example = "1")
    @NotBlank
    private Long id;

    @Schema(description = "구분", example = "관리자/라벨러/검수자 중 하나")
    @NotBlank
    private String userAuth;

    @NotBlank
    @Schema(description = "이름", example = "홍길동")
    private String userNm;

    @Schema(description = "ID", example = "gildong")
    @NotBlank
    private String userId;

    @Schema(description = "PW", example = "password")
    @NotBlank
    private String userPw;

    @Schema(description = "사번", example = "사번")
    @NotBlank
    private String empId;

    @Schema(description = "이메일", example = "gildong@naver.com")
    @NotBlank
    private String userEmail;

    public UpdateReq(
        Long id,
        String userAuth,
        String userNm,
        String userId,
        String userPw,
        String empId,
        String userEmail) {
      this.id = id;
      this.userAuth = userAuth;
      this.userNm = userNm;
      this.userId = userId;
      this.userPw = userPw;
      this.empId = empId;
      this.userEmail = userEmail;
    }
  }

  @Getter
  public static class User {

    String userId;
    String userPw;
  }

  @Schema(name = "UserSearchReq", description = "관리자 목록 요청 정보")
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class SearchReq {

    // 검색 조건
    private String userNm;

    // 페이징 파라미터
    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);
    }
  }
}
