package org.ta.authserver.component;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 自定义用户服务（测试用，生产替换为数据库）
 */
public class CustomUserDetailsService implements UserDetailsService {

    private final PasswordEncoder passwordEncoder;

    public CustomUserDetailsService(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 测试用户：admin/admin123 | user/user123
        if ("admin".equals(username)) {
            return User.withUsername("admin")
                    .password(passwordEncoder.encode("admin123"))
                    .roles("ADMIN")
                    .authorities("read", "write")
                    .accountExpired(false)
                    .accountLocked(false)
                    .credentialsExpired(false)
//                    .enabled(true)
                    .build();
        } else if ("user".equals(username)) {
            return User.withUsername("user")
                    .password(passwordEncoder.encode("user123"))
                    .roles("USER")
                    .authorities("read")
                    .accountExpired(false)
                    .accountLocked(false)
                    .credentialsExpired(false)
//                    .enabled(true)
                    .build();
        } else {
            throw new UsernameNotFoundException("用户不存在：" + username);
        }
    }
}
