package com.dbnt.faisp.userInfo.service;

import com.dbnt.faisp.config.Role;
import com.dbnt.faisp.userInfo.mapper.UserInfoMapper;
import com.dbnt.faisp.userInfo.model.UserInfo;
import com.dbnt.faisp.userInfo.repository.UserInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserInfoService implements UserDetailsService {

  private final UserInfoRepository userInfoRepository;
  private final UserInfoMapper userInfoMapper;

  @Transactional
  public String insertUserInfo(UserInfo userInfo){
    if(userInfoRepository.findByUserId(userInfo.getUserId()).orElse(null) != null){
      return "userIdDuplication";
    }
    userInfo.setUserRole(Role.USER.getValue());
    userInfo.setUserStatus("USC002");
    userInfo.setPassword(convertPassword(userInfo.getPassword()));
    userInfo.setWrtDt(LocalDateTime.now());
    return userInfoRepository.save(userInfo).getUserId();
  }
  @Transactional
  public String updateUserInfo(UserInfo userInfo){
    userInfo.setPassword(convertPassword(userInfo.getPassword()));
    return userInfoRepository.save(userInfo).getUserId();
  }

  public String updatePassword(UserInfo loginUser, UserInfo modifyInfo){
    Pbkdf2PasswordEncoder passwordEncoder = new Pbkdf2PasswordEncoder();
    if(passwordEncoder.matches(modifyInfo.getPassword(), loginUser.getPassword())){
      loginUser.setPassword(convertPassword(modifyInfo.getModifyPassword()));
      userInfoRepository.save(loginUser);
      return "OK";
    }else{
      return "passwordNotMatch";
    }
  }

  private String convertPassword(String password){
    Pbkdf2PasswordEncoder passwordEncoder = new Pbkdf2PasswordEncoder();
    return passwordEncoder.encode(password);
  }

  @Override
  public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
    return userInfoRepository.findByUserId(userId).orElseThrow(() -> new UsernameNotFoundException(userId));
  }

  public List<UserInfo> selectUserInfoList(UserInfo userInfo) {
    return userInfoMapper.selectUserInfoList(userInfo);
  }

  public Integer selectUserInfoListCnt(UserInfo userInfo) {
    return userInfoMapper.selectUserInfoListCnt(userInfo);
  }
  
  public UserInfo selectUserInfo(Integer userSeq) {
	return userInfoRepository.findByUserSeq(userSeq);
  }
  @Transactional
  public int updateUserApproval(List<UserInfo> userInfo) {
	 int cnt = 0;
	for(UserInfo user: userInfo) {
		UserInfo dbUserInfo = userInfoRepository.findById(user.getUserSeq()).orElse(null);
		if(dbUserInfo != null) {
			dbUserInfo.setUserStatus(user.getUserStatus());
			userInfoRepository.save(dbUserInfo);
			cnt++;
		}
	}
	return cnt;
  }
  @Transactional
  public int updateUserCompanion(List<UserInfo> userInfo) {
	int cnt = 0;
	for(UserInfo user: userInfo) {
		UserInfo dbUserInfo = userInfoRepository.findById(user.getUserSeq()).orElse(null);
		if(dbUserInfo != null) {
			dbUserInfo.setUserStatus(user.getUserStatus());
			userInfoRepository.save(dbUserInfo);
			cnt++;
		}
	}
	return cnt;
  }
  @Transactional
  public void updateUser(UserInfo userInfo) { 
	  UserInfo dbUserInfo = userInfoRepository.findById(userInfo.getUserSeq()).orElse(null);
	  dbUserInfo.setUserRole(userInfo.getUserRole());
	  userInfoRepository.save(dbUserInfo);
		
  }
  public void userDelete(List<UserInfo> userInfo) {
	  userInfoRepository.deleteAll(userInfo);	
  }
}
