package de.effigenix.infrastructure.security;

import de.effigenix.domain.usermanagement.User;
import de.effigenix.domain.usermanagement.UserId;
import de.effigenix.domain.usermanagement.UserRepository;
import de.effigenix.shared.security.UserLookupPort;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * Spring implementation of UserLookupPort.
 *
 * Resolves user IDs to usernames via UserRepository.
 */
@Component
public class SpringUserLookupAdapter implements UserLookupPort {

    private final UserRepository userRepository;

    public SpringUserLookupAdapter(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public Optional<String> resolveUsername(String userId) {
        return userRepository.findById(UserId.of(userId))
                .fold(
                        err -> Optional.empty(),
                        opt -> opt.map(User::username)
                );
    }
}
