package com.dbnt.kcgfilemanager.service;

import com.dbnt.kcgfilemanager.mapper.UserInfoMapper;
import com.dbnt.kcgfilemanager.model.UserInfo;
import com.dbnt.kcgfilemanager.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.util.List;

@Service
@RequiredArgsConstructor
public class UserInfoService implements UserDetailsService {

  private final UserInfoRepository userInfoRepository;
  private final UserInfoMapper userInfoMapper;

  @Transactional
  public String insertUserInfo(UserInfo userInfo){
    userInfo.setPassword(convertPassword(userInfo.getPassword()));
    return userInfoRepository.save(userInfo).getUserId();
  }
  @Transactional
  public String updateUserInfo(UserInfo userInfo){
    UserInfo targetUserInfo = userInfoRepository.findById(userInfo.getUserSeq()).orElse(null);
    if(userInfo.getPassword() != null){
      targetUserInfo.setPassword(convertPassword(userInfo.getPassword()));
    }
    targetUserInfo.setName(userInfo.getName());
    targetUserInfo.setPosition(userInfo.getPosition());
    targetUserInfo.setDepartment(userInfo.getDepartment());
    targetUserInfo.setUserRole(userInfo.getUserRole());
    targetUserInfo.setUserStatus(userInfo.getUserStatus());
    return targetUserInfo.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 selectUserInfoByUserSeq(UserInfo userInfo) {
    return userInfoRepository.findById(userInfo.getUserSeq()).orElse(null);
  }
  public UserInfo selectUserInfoByUserId(String userId) {
    return userInfoRepository.findByUserId(userId).orElse(null);
  }
}
