package com.bsmlab.dfx.dfxconsole.framework.config;

import com.bsmlab.dfx.dfxconsole.app.user.DfxUserDto;
import com.bsmlab.dfx.dfxconsole.app.user.DfxUserMapper;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class AppUserDetailsService implements UserDetailsService {
    private final DfxUserMapper dfxUserMapper;
    @Override
    public UserDetails loadUserByUsername(String userEmail) throws UsernameNotFoundException {
        DfxUserDto dfxUserDto = dfxUserMapper.selectConsoleUserByUserEmail(DfxUserDto.builder().userEmail(userEmail).build());
        if(dfxUserDto == null || StringUtils.isBlank(dfxUserDto.getUserUuid())) {
            throw new UsernameNotFoundException("사용자 정보를 찾을 수 없습니다.");
        }
        return new AppUserDetails(dfxUserDto);
    }
}
