package ba.unsa.etf.si.bbqms.repository.specs;

import ba.unsa.etf.si.bbqms.domain.Ticket;
import org.springframework.data.jpa.domain.Specification;

import java.time.Instant;
import java.util.Collection;

public final class TicketSpecs {
    private TicketSpecs() {
    }

    public static Specification<Ticket> branchIdEquals(final long branchId) {
        return (root, query, cb) -> cb.equal(root.get("branch").get("id"), branchId);
    }

    public static Specification<Ticket> serviceIdEquals(final Long serviceId) {
        return (root, query, cb) -> cb.equal(root.get("service").get("id"), serviceId);
    }

    public static Specification<Ticket> serviceIdIn(final Collection<Long> serviceIds) {
        return (root, query, cb) -> root.get("service").get("id").in(serviceIds);
    }

    public static Specification<Ticket> tellerStationIdEquals(final Long stationId) {
        if (stationId == null) {
            return (root, query, cb) -> cb.isNull(root.get("tellerStation"));
        }
        return (root, query, cb) -> cb.equal(root.get("tellerStation").get("id"), stationId);
    }

    public static Specification<Ticket> deletedEquals(final boolean deleted) {
        return (root, query, cb) -> cb.equal(root.get("deleted"), deleted);
    }

    public static Specification<Ticket> deviceTokenEquals(final String deviceToken) {
        return (root, query, cb) -> cb.equal(root.get("deviceToken"), deviceToken);
    }

    public static Specification<Ticket> createdAtGreaterThanOrEqual(final Instant createdAfter) {
        return (root, query, cb) -> cb.greaterThanOrEqualTo(root.get("createdAt"), createdAfter);
    }

    public static Specification<Ticket> createdAtLessThanOrEqual(final Instant createdBefore) {
        return (root, query, cb) -> cb.lessThanOrEqualTo(root.get("createdAt"), createdBefore);
    }

    public static Specification<Ticket> createdAtLessThan(final Instant createdAt) {
        return (root, query, cb) -> cb.lessThan(root.get("createdAt"), createdAt);
    }
}
