package com.its.vms.webapp.service;

import com.its.vms.webapp.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class UserService implements UserDetailsService {

    @Value("${server.user.id:admin}")
    private String userId;
    @Value("${server.user.pswd:1234}")
    private String userPswd;

    @Override
    public UserVo loadUserByUsername(String username) throws UsernameNotFoundException {

        String userId = this.userId;
        String userPswd = this.userPswd;
        UserVo user = new UserVo(userId,"{noop}"+userPswd);
        if (user == null) {
            throw new UsernameNotFoundException("No user found with "+username + user.getUsername());
        }

        //디비에서 권한 가져와야하는데 고정으로 넣어줌
        List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
        roles.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        user.setAuthorities(roles);
        return user;
    }
}
