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

import com.kamco.cd.kamcoback.common.enums.RoleType;
import com.kamco.cd.kamcoback.common.enums.StatusType;
import com.kamco.cd.kamcoback.common.utils.interfaces.JsonFormatDttm;
import com.kamco.cd.kamcoback.config.enums.EnumType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.time.ZonedDateTime;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

public class MembersDto {

  @Getter
  @Setter
  public static class Basic {

    private Long id;
    private UUID uuid;
    private String userRole;
    private String userRoleName;
    private String name;
    private String userId;
    private String employeeNo;
    private String tempPassword;
    private String status;
    private String statusName;
    @JsonFormatDttm private ZonedDateTime createdDttm;
    @JsonFormatDttm private ZonedDateTime updatedDttm;
    @JsonFormatDttm private ZonedDateTime firstLoginDttm;
    @JsonFormatDttm private ZonedDateTime lastLoginDttm;

    public Basic(
        Long id,
        UUID uuid,
        String userRole,
        String name,
        String userId,
        String employeeNo,
        String tempPassword,
        String status,
        ZonedDateTime createdDttm,
        ZonedDateTime updatedDttm,
        ZonedDateTime firstLoginDttm,
        ZonedDateTime lastLoginDttm) {
      this.id = id;
      this.uuid = uuid;
      this.userRole = userRole;
      this.userRoleName = getUserRoleName(userRole);
      this.name = name;
      this.userId = userId;
      this.employeeNo = employeeNo;
      this.tempPassword = tempPassword;
      this.status = status;
      this.statusName = getStatusName(status);
      this.createdDttm = createdDttm;
      this.updatedDttm = updatedDttm;
      this.firstLoginDttm = firstLoginDttm;
      this.lastLoginDttm = lastLoginDttm;
    }

    private String getUserRoleName(String roleId) {
      RoleType type = EnumType.fromId(RoleType.class, roleId);
      return type.getText();
    }

    private String getStatusName(String status) {
      StatusType type = EnumType.fromId(StatusType.class, status);
      return type.getText();
    }
  }

  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class SearchReq {

    @Schema(
        description = "전체, 관리자(ROLE_ADMIN), 라벨러(ROLE_LABELER), 검수자(ROLE_REVIEWER)",
        example = "")
    private String userRole;

    @Schema(description = "키워드", example = "홍길동")
    private String keyword;

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

  @Getter
  @Setter
  public static class AddReq {

    @Schema(description = "관리자 유형", example = "ROLE_ADMIN")
    @NotBlank
    @Size(max = 50)
    private String userRole;

    @Schema(description = "이름", example = "홍길동")
    @NotBlank
    @Size(min = 2, max = 100)
    private String name;

    @Schema(description = "ID", example = "gildong")
    @NotBlank
    @Size(min = 2, max = 50)
    private String userId;

    @Schema(description = "임시 비밀번호", example = "q!w@e#r4")
    private String tempPassword;

    @Schema(description = "사번", example = "123456")
    private String employeeNo;

    public AddReq(
        String userRole, String name, String userId, String tempPassword, String employeeNo) {
      this.userRole = userRole;
      this.name = name;
      this.userId = userId;
      this.tempPassword = tempPassword;
      this.employeeNo = employeeNo;
    }
  }

  @Getter
  @Setter
  public static class UpdateReq {

    @Schema(description = "사번, 패스워드 변경시 필수 값", example = "11111")
    @Size(max = 50)
    private String employeeNo;

    @Schema(description = "이름", example = "홍길동")
    @Size(min = 2, max = 100)
    private String name;

    @Schema(description = "패스워드", example = "")
    @Size(max = 255)
    private String tempPassword;

    public UpdateReq(String employeeNo, String name, String tempPassword) {
      this.employeeNo = employeeNo;
      this.name = name;
      this.tempPassword = tempPassword;
    }
  }

  @Getter
  @Setter
  public static class InitReq {

    @Schema(description = "변경 패스워드", example = "")
    @Size(max = 255)
    @NotBlank
    private String password;

    @Schema(description = "초기 패스워드", example = "")
    @NotBlank
    private String tempPassword;
  }
}
