package com.lingniu.ingest.tdenginehistory;

import java.time.Instant;

public record TdengineLocationQuery(
        String protocol,
        String vehicleKey,
        Instant from,
        Instant to,
        TdengineQueryOrder order,
        int limit,
        TdenginePageCursor cursor
) {
    public TdengineLocationQuery {
        if (protocol == null || protocol.isBlank()) {
            throw new IllegalArgumentException("protocol must not be blank");
        }
        if (vehicleKey == null || vehicleKey.isBlank()) {
            throw new IllegalArgumentException("vehicleKey must not be blank");
        }
        if (from == null || to == null || !from.isBefore(to)) {
            throw new IllegalArgumentException("query time range must be valid");
        }
        order = order == null ? TdengineQueryOrder.DESC : order;
        limit = Math.max(1, Math.min(limit, 1001));
    }

    TdengineLocationQuery withLimit(int newLimit) {
        return new TdengineLocationQuery(protocol, vehicleKey, from, to, order, newLimit, cursor);
    }
}
