package dev.dinauer.maven.app;

import dev.dinauer.maven.maven.core.ArtifactId;
import dev.dinauer.maven.maven.core.GroupId;
import dev.dinauer.maven.shared.UserReferencing;
import dev.dinauer.maven.user.User;
import dev.dinauer.maven.user.UserClient;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@ApplicationScoped
public class ArtifactMapper
{
    @Inject
    VersionMapper versionMapper;

    @RestClient
    UserClient userClient;

    public List<ArtifactDTO> map(List<ArtifactId> input)
    {
        return input.stream().map(artifact -> this.map(artifact, userClient.get(UserReferencing.collect(input)))).toList();
    }

    public List<ArtifactDTO> map(List<ArtifactId> input, Map<String, User> users)
    {
        return input.stream().map(artifact -> this.map(artifact, users)).toList();
    }

    public ArtifactDTO map(ArtifactId input)
    {
        return map(input, userClient.get(input.getUserIds()));
    }

    public ArtifactDTO map(ArtifactId input, Map<String, User> users)
    {
        return new ArtifactDTO(input.getId(), input.getGroup().getGroupId(), input.getArtifactId(), versionMapper.map(input.getVersions(), users));
    }
}