package at.procon.dip.clustering.repository;

import at.procon.dip.clustering.ClusterRunStatus;
import at.procon.dip.clustering.ClusteringAlgorithm;
import at.procon.dip.clustering.ClusteringExecutionBackend;
import at.procon.dip.clustering.entity.EmbeddingClusterRun;
import at.procon.dip.domain.document.DocumentType;
import java.time.OffsetDateTime;
import org.springframework.data.jpa.domain.Specification;

public final class EmbeddingClusterRunSpecifications {

    private EmbeddingClusterRunSpecifications() {
    }

    public static Specification<EmbeddingClusterRun> hasStatus(ClusterRunStatus status) {
        return (root, query, cb) -> status == null ? null : cb.equal(root.get("status"), status);
    }

    public static Specification<EmbeddingClusterRun> hasAlgorithm(ClusteringAlgorithm algorithm) {
        return (root, query, cb) -> algorithm == null ? null : cb.equal(root.get("algorithm"), algorithm);
    }

    public static Specification<EmbeddingClusterRun> hasExecutionBackend(ClusteringExecutionBackend executionBackend) {
        return (root, query, cb) -> executionBackend == null ? null : cb.equal(root.get("executionBackend"), executionBackend);
    }

    public static Specification<EmbeddingClusterRun> hasDocumentType(DocumentType documentType) {
        return (root, query, cb) -> documentType == null ? null : cb.equal(root.get("documentType"), documentType);
    }

    public static Specification<EmbeddingClusterRun> nameContains(String value) {
        return (root, query, cb) -> value == null || value.isBlank()
                ? null
                : cb.like(cb.lower(root.get("name")), "%" + value.toLowerCase() + "%");
    }

    public static Specification<EmbeddingClusterRun> createdAtFrom(OffsetDateTime value) {
        return (root, query, cb) -> value == null ? null : cb.greaterThanOrEqualTo(root.get("createdAt"), value);
    }

    public static Specification<EmbeddingClusterRun> createdAtTo(OffsetDateTime value) {
        return (root, query, cb) -> value == null ? null : cb.lessThan(root.get("createdAt"), value);
    }
}
