package dev.dinauer.maven.app;

import dev.dinauer.maven.maven.core.Version;
import dev.dinauer.maven.maven.core.release.ReleaseVersion;
import dev.dinauer.maven.maven.core.snapshot.SnapshotVersion;
import dev.dinauer.maven.user.User;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

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

@ApplicationScoped
public class VersionMapper
{
    @Inject
    PomMapper pomMapper;

    @Inject
    JarMapper jarMapper;

    public List<VersionDTO> map(List<Version> input, Map<String, User> users)
    {
        return input.stream().map(version -> this.map(version, users)).toList();
    }

    public VersionDTO map(Version input, Map<String, User> users)
    {
        if (input instanceof ReleaseVersion releaseVersion)
        {
            return new ReleaseVersionDTO(releaseVersion.getId(), releaseVersion.getVersion(), pomMapper.map(releaseVersion.getPom()), jarMapper.map(releaseVersion.getJars()), users.get(releaseVersion.getUploadedBy()));
        }
        if (input instanceof SnapshotVersion snapshotVersion)
        {
            return new SnapshotVersionDTO(snapshotVersion.getId(), snapshotVersion.getVersion());
        }
        throw new IllegalStateException();
    }
}
