package dev.dinauer.maven.maven.core.release;

import dev.dinauer.maven.maven.core.ArtifactService;
import dev.dinauer.maven.maven.core.context.VersionContext;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

@ApplicationScoped
public class ReleaseVersionService
{
    @Inject
    ReleaseVersionRepo releaseVersionRepo;

    @Inject
    ArtifactService artifactService;

    public ReleaseVersion findOrCreate(VersionContext versionContext)
    {
        ReleaseVersion releaseVersion = releaseVersionRepo.findByGroupIdAndArtifactIdAndVersion(versionContext.groupId(), versionContext.artifactId(), versionContext.version());
        if (releaseVersion != null)
        {
            return releaseVersion;
        }
        else
        {
            ReleaseVersion created = new ReleaseVersion();
            created.setArtifact(artifactService.findOrCreate(versionContext.groupId(), versionContext.artifactId()));
            created.setVersion(versionContext.version());
            releaseVersionRepo.persist(created);
            return created;
        }
    }
}
