package com.example.items.service;

import com.example.items.entity.Employee;
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;
import org.springframework.transaction.annotation.Transactional;

@Service
public class LoginService implements UserDetailsService {

    @Autowired
    private EmployeeService employeeService;

    @Override
    @Transactional
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        Employee employee = employeeService.findByEmail(email);
        if (employee == null) {
            throw new UsernameNotFoundException("Email (username) not found");
        }
        return new org.springframework.security.core.userdetails.User(employee.getEmail(), employee.getPasscode(), employee.getAuthorities());
    }


}
