package ru.kayashov.karthall.security;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
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.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.kayashov.karthall.model.entity.UserRole;
import ru.kayashov.karthall.repository.PlayerRepository;

import java.util.Set;

@Component
@RequiredArgsConstructor
public class CustomUserDetailService implements UserDetailsService {

    private final PlayerRepository repository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return repository.findById(Long.parseLong(username)).orElseThrow(() -> new RuntimeException("Пользователь с email " + username + " не найден!"));
    }

    @Transactional
    public Set<GrantedAuthority> getAuthorities(long userId) {
//        Visitor visitor = repository.findById(userId).orElseThrow(RuntimeException::new);
        return UserRole.ADMIN.getAuthorities();
//        return visitor.getResidents().stream()
//                .filter(BarResident::getActive)
//                .map(BarResident::getRole)
//                .map(UserRole::getAuthorities)
//                .flatMap(Collection::stream)
//                .collect(Collectors.toSet());
    }
}
