package com.cameleer.server.core.search;

import java.time.Instant;
import java.util.List;

/**
 * Immutable search criteria for querying application logs.
 *
 * @param q           free-text search across message and stack trace
 * @param levels      log level filter (e.g. ["WARN","ERROR"]), OR-joined
 * @param application application ID filter (nullable = all apps)
 * @param instanceId  agent instance ID filter (single value; coexists with instanceIds)
 * @param exchangeId  Camel exchange ID filter
 * @param logger      logger name substring filter
 * @param environment optional environment filter (e.g. "dev", "staging", "prod")
 * @param sources     optional source filter (e.g. ["app","container","agent"]), OR-joined
 * @param from        inclusive start of time range
 * @param to          inclusive end of time range
 * @param cursor      ISO timestamp cursor for keyset pagination
 * @param limit       page size (1-500, default 100)
 * @param sort        sort direction: "asc" or "desc" (default "desc")
 * @param instanceIds multi-value instance ID filter (IN clause); scopes logs to one deployment's
 *                    replicas when provided. Both instanceId and instanceIds may coexist — both
 *                    conditions apply (AND). Empty/null means no additional filtering.
 */
public record LogSearchRequest(
        String q,
        List<String> levels,
        String application,
        String instanceId,
        String exchangeId,
        String logger,
        String environment,
        List<String> sources,
        Instant from,
        Instant to,
        String cursor,
        int limit,
        String sort,
        List<String> instanceIds
) {

    private static final int DEFAULT_LIMIT = 100;
    private static final int MAX_LIMIT = 500;

    public LogSearchRequest {
        if (limit <= 0) limit = DEFAULT_LIMIT;
        if (limit > MAX_LIMIT) limit = MAX_LIMIT;
        if (sort == null || !"asc".equalsIgnoreCase(sort)) sort = "desc";
        if (levels == null) levels = List.of();
        if (sources == null) sources = List.of();
        if (instanceIds == null) instanceIds = List.of();
    }
}
