package kr.iotdoor.boot.service;

import kr.iotdoor.boot.mapper.UserMapper;
import kr.iotdoor.boot.model.user.AdminUserPrincipal;
import kr.iotdoor.boot.model.user.UserAccount;
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
public class AdminUserDetailsService implements UserDetailsService {

    private final UserMapper userMapper;

    public AdminUserDetailsService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserAccount userAccount = userMapper.selectByUserId(username);
        if (userAccount == null) {
            throw new UsernameNotFoundException("관리자 계정을 찾을 수 없습니다.");
        }

        return new AdminUserPrincipal(userAccount);
    }
}
