package dev.dinauer.maven.app;

import dev.dinauer.maven.maven.core.ArtifactRepo;
import dev.dinauer.maven.user.User;
import dev.dinauer.maven.maven.core.release.ReleaseVersion;
import dev.dinauer.maven.maven.core.ArtifactId;
import dev.dinauer.maven.user.UserClient;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Path("/artifacts")
public class ArtifactResource
{
    @Inject
    ArtifactRepo artifactRepo;

    @RestClient
    UserClient userClient;

    @GET
    public List<ArtifactId> get()
    {
        return find();
    }

    @GET
    @Path("/{id}")
    public ArtifactId getById(@PathParam("id") String id)
    {
        return artifactRepo.findById(id);
    }

    @DELETE
    @Path("/{id}")
    @Transactional
    public void deleteById(@PathParam("id") String id)
    {
        artifactRepo.deleteById(id);
    }

    private List<ArtifactId> find()
    {
        List<ArtifactId> artifactIds = artifactRepo.listAll();
        Map<String, User> accounts = getAccounts(artifactIds);
        for (ArtifactId artifactId : artifactIds)
        {
            System.out.println("Skip setting user. Needs to be implemented");
        }
        return artifactIds;
    }

    private Map<String, User> getAccounts(List<ArtifactId> artifactIds)
    {
        List<String> ids = new ArrayList<>();
        for (ArtifactId artifactId : artifactIds)
        {
            System.out.println("Skip setting user. Needs to be implemented");
        }
        return userClient.get(ids);
    }
}
