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.Enums;
import com.kamco.cd.kamcoback.common.utils.interfaces.EnumValid;
import com.kamco.cd.kamcoback.common.utils.interfaces.JsonFormatDttm;
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 employeeNo;
    private String status;
    private String statusName;
    @JsonFormatDttm private ZonedDateTime createdDttm;
    @JsonFormatDttm private ZonedDateTime firstLoginDttm;
    @JsonFormatDttm private ZonedDateTime lastLoginDttm;
    @JsonFormatDttm private ZonedDateTime statusChgDttm;

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

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

    private String getStatusName(String status, Boolean pwdResetYn) {
      StatusType type = Enums.fromId(StatusType.class, status);
      pwdResetYn = pwdResetYn != null && pwdResetYn;
      if (type.equals(StatusType.PENDING) && pwdResetYn) {
        type = StatusType.ACTIVE;
      }
      return type.getText();
    }
  }

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

    @Schema(description = "전체, 관리자(ADMIN), 라벨러(LABELER), 검수자(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 = "ADMIN")
    @NotBlank
    @EnumValid(enumClass = RoleType.class, message = "userRole은 ADMIN, LABELER, REVIEWER 만 가능합니다.")
    private String userRole;

    @Schema(description = "사번", example = "K20251212001")
    @Size(max = 50)
    private String employeeNo;

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

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

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

  @Getter
  @Setter
  public static class UpdateReq {

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

    @Schema(description = "상태", example = "ACTIVE")
    @EnumValid(enumClass = StatusType.class, message = "status는 ACTIVE, INACTIVE, DELETED 만 가능합니다.")
    private String status;

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

    public UpdateReq(String name, String status, String password) {
      this.name = name;
      this.status = status;
      this.password = password;
    }
  }

  @Getter
  @Setter
  public static class InitReq {

    @Schema(description = "기존 패스워드", example = "")
    @Size(max = 255)
    @NotBlank
    private String oldPassword;

    @Schema(description = "신규 패스워드", example = "")
    @Size(max = 255)
    @NotBlank
    private String newPassword;
  }

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

    private Long id;
    private String name;
    private String employeeNo;
  }
}
