package dev.ksan.etfoglasiserver.service;

import dev.ksan.etfoglasiserver.model.User;
import dev.ksan.etfoglasiserver.model.UserPrincipal;
import dev.ksan.etfoglasiserver.repository.UserRepo;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
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 MyUserDetailsService implements UserDetailsService {

  @Autowired private UserRepo userRepo;

  @Override
  public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {

      Optional<User> user = userRepo.findByEmail(email);
      if (!user.isPresent()) {
        throw new UsernameNotFoundException("user not found");
      }

      return new UserPrincipal(user.get());

  }
}

