package dev.dinauer.maven.maven.core.snapshot;

import dev.dinauer.maven.maven.core.context.VersionContext;
import dev.dinauer.maven.maven.core.snapshot.metadata.utils.SnapshotComparator;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;

import java.util.Map;

@ApplicationScoped
public class SnapshotBundleRepo implements PanacheRepositoryBase<SnapshotBundle, String>
{
    public SnapshotBundle findByVersionAndProperties(SnapshotVersion version, String date, String time, int buildNumber)
    {
        return find("snapshotVersion = :snapshotVersion AND date = :date AND time = :time AND buildNumber = :buildNumber", Map.ofEntries(Map.entry("snapshotVersion", version), Map.entry("date", date), Map.entry("time", time), Map.entry("buildNumber", buildNumber))).firstResult();
    }

    public SnapshotBundle latest(VersionContext versionContext)
    {
        return list("snapshotVersion.version = ?1 AND snapshotVersion.artifact.artifactId = ?2 AND snapshotVersion.artifact.group.groupId = ?3", versionContext.version(), versionContext.artifactId(), versionContext.groupId()).stream()
            .sorted(new SnapshotComparator())
            .toList()
            .getFirst();
    }
}
