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

import com.kamco.cd.kamcoback.auth.BCryptSaltGenerator;
import com.kamco.cd.kamcoback.members.dto.MembersDto;
import com.kamco.cd.kamcoback.postgres.core.MembersCoreService;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

  private final MembersCoreService membersCoreService;

  @Value("${member.init_password}")
  private String password;

  /**
   * 회원가입
   *
   * @param addReq
   * @return
   */
  @Transactional
  public Long saveMember(MembersDto.AddReq addReq) {
    // salt 생성, 사번이 salt
    String salt = BCryptSaltGenerator.generateSaltWithEmployeeNo(addReq.getEmployeeNo().trim());

    // 패스워드 암호화, 초기 패스워드 고정
    String hashedPassword = BCrypt.hashpw(password, salt);
    addReq.setPassword(hashedPassword);
    return membersCoreService.saveMembers(addReq);
  }

  /**
   * 역할 추가
   *
   * @param rolesDto
   */
  @Transactional
  public void saveRoles(MembersDto.RolesDto rolesDto) {
    membersCoreService.saveRoles(rolesDto);
  }

  /**
   * 역할 삭제
   *
   * @param rolesDto
   */
  public void deleteRoles(MembersDto.RolesDto rolesDto) {
    membersCoreService.deleteRoles(rolesDto);
  }

  /**
   * 역할 수정
   *
   * @param statusDto
   */
  public void updateStatus(UUID uuid, MembersDto.StatusDto statusDto) {
    membersCoreService.updateStatus(uuid, statusDto);
  }

  /**
   * 회원 탈퇴
   *
   * @param uuid
   */
  public void deleteAccount(UUID uuid) {
    membersCoreService.deleteAccount(uuid);
  }

  /**
   * 패스워드 초기화
   *
   * @param id
   */
  public void resetPassword(Long id) {
    membersCoreService.resetPassword(id);
  }
}
