package com.kamco.cd.training.members.service;

import com.kamco.cd.training.common.exception.CustomApiException;
import com.kamco.cd.training.common.utils.CommonStringUtils;
import com.kamco.cd.training.members.dto.MembersDto;
import com.kamco.cd.training.members.dto.MembersDto.Basic;
import com.kamco.cd.training.postgres.core.MembersCoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MembersService {

  private final MembersCoreService membersCoreService;

  /**
   * 회원목록 조회
   *
   * @param searchReq
   * @return
   */
  public Page<Basic> findByMembers(MembersDto.SearchReq searchReq) {
    return membersCoreService.findByMembers(searchReq);
  }

  /**
   * 패스워드 사용자 변경
   *
   * @param id
   * @param initReq
   */
  @Transactional
  public void resetPassword(String id, MembersDto.InitReq initReq) {

    if (!CommonStringUtils.isValidPassword(initReq.getNewPassword())) {
      throw new CustomApiException("WRONG_PASSWORD", HttpStatus.BAD_REQUEST);
    }
    membersCoreService.resetPassword(id, initReq);
  }
}
