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

import dev.dinauer.maven.maven.core.context.ReleaseContext;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.NotFoundException;
import org.apache.commons.lang3.NotImplementedException;

import java.util.Map;

@ApplicationScoped
public class ReleasePomRepo implements PanacheRepositoryBase<ReleasePom, String>
{
    @Inject
    ReleaseVersionRepo releaseVersionRepo;

    public ReleasePom find(ReleaseContext releaseContext)
    {
        ReleaseVersion version = releaseVersionRepo.findByGroupIdAndArtifactIdAndVersion(releaseContext.groupId(), releaseContext.artifactId(), releaseContext.version());
        if (version != null)
        {
            return find("releaseVersion = :version", Map.ofEntries(Map.entry("version", version))).firstResult();
        }
        return null;
    }

    public ReleasePom require(ReleaseContext releaseContext)
    {
        ReleaseVersion version = releaseVersionRepo.requireByGroupIdAndArtifactIdAndVersion(releaseContext.groupId(), releaseContext.artifactId(), releaseContext.version());
        ReleasePom releasePom = find("releaseVersion = :version", Map.ofEntries(Map.entry("version", version))).firstResult();
        if (releasePom != null)
        {
            return releasePom;
        }
        else
        {
            throw new NotFoundException();
        }
    }
}
