package de.rehpoehler.user.services;

import de.rehpoehler.user.model.UserAccount;
import de.rehpoehler.user.model.AccountRepository;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional(readOnly = true)
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {

	@Autowired
	private AccountRepository accountRepo;

	@Override
	public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
		log.debug("loading user <{}>", email);
		UserAccount account = accountRepo.findByEmail(email);

		if (null == account) {
			throw new UsernameNotFoundException("Username <" + email + "> not found");
		}

		return account;
	}
}
