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

import dev.dinauer.maven.maven.core.ArtifactService;
import dev.dinauer.maven.maven.core.context.VersionContext;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.SecurityContext;

@ApplicationScoped
public class ReleaseVersionService
{
    @Inject
    ReleaseVersionRepo releaseVersionRepo;

    @Inject
    ArtifactService artifactService;

    @Inject
    SecurityIdentity securityIdentity;

    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());
            created.setUploadedBy(securityIdentity.getPrincipal().getName());
            releaseVersionRepo.persist(created);
            return created;
        }
    }
}
