/**
 * Spring Security 用户信息加载服务。
 *
 * <p>用于根据用户名加载用户信息，供认证与权限体系使用。</p>
 */
package com.hertz.security;

import com.hertz.modules.system.service.UserService;
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 CustomUserDetailsService implements UserDetailsService {

    private final UserService userService;

    public CustomUserDetailsService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        var user = userService.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("User not found: " + username);
        }
        // We are using JWT for auth, so this is mostly to satisfy Spring Security's default config
        // or if we wanted to support Basic Auth/Form Login alongside JWT.
        // For now, we return a minimal UserDetails implementation.
        return org.springframework.security.core.userdetails.User
            .withUsername(user.getUsername())
            .password(user.getPassword())
            .roles("USER") // Default role, actual authorities are loaded in JwtAuthFilter
            .build();
    }
}
