package com.cameleer.server.core.storage;

import com.cameleer.server.core.search.ExecutionStats;
import com.cameleer.server.core.search.StatsTimeseries;
import com.cameleer.server.core.search.TopError;

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

public interface StatsStore {

    // Global stats (stats_1m_all)
    ExecutionStats stats(Instant from, Instant to, String environment);

    // Per-app stats (stats_1m_app)
    ExecutionStats statsForApp(Instant from, Instant to, String applicationId, String environment);

    // Per-route stats (stats_1m_route), optionally scoped to an application
    ExecutionStats statsForRoute(Instant from, Instant to, String routeId, String applicationId, String environment);

    // Per-processor stats (stats_1m_processor)
    ExecutionStats statsForProcessor(Instant from, Instant to, String routeId, String processorType);

    // Global timeseries
    StatsTimeseries timeseries(Instant from, Instant to, int bucketCount, String environment);

    // Per-app timeseries
    StatsTimeseries timeseriesForApp(Instant from, Instant to, int bucketCount, String applicationId, String environment);

    // Per-route timeseries, optionally scoped to an application
    StatsTimeseries timeseriesForRoute(Instant from, Instant to, int bucketCount,
                                        String routeId, String applicationId, String environment);

    // Per-processor timeseries
    StatsTimeseries timeseriesForProcessor(Instant from, Instant to, int bucketCount,
                                            String routeId, String processorType);

    // Grouped timeseries by application (for L1 dashboard charts)
    Map<String, StatsTimeseries> timeseriesGroupedByApp(Instant from, Instant to, int bucketCount, String environment);

    // Grouped timeseries by route within an application (for L2 dashboard charts)
    Map<String, StatsTimeseries> timeseriesGroupedByRoute(Instant from, Instant to, int bucketCount,
                                                           String applicationId, String environment);

    // SLA compliance: % of completed exchanges with duration <= thresholdMs
    double slaCompliance(Instant from, Instant to, int thresholdMs,
                         String applicationId, String routeId, String environment);

    // Batch SLA counts by app: {appId -> [compliant, total]}
    Map<String, long[]> slaCountsByApp(Instant from, Instant to, int defaultThresholdMs, String environment);

    // Batch SLA counts by route within an app: {routeId -> [compliant, total]}
    Map<String, long[]> slaCountsByRoute(Instant from, Instant to, String applicationId,
                                          int thresholdMs, String environment);

    // Top N errors with velocity trend
    List<TopError> topErrors(Instant from, Instant to, String applicationId,
                             String routeId, int limit, String environment);

    // Count of distinct error types in window
    int activeErrorTypes(Instant from, Instant to, String applicationId, String environment);

    // Punchcard: aggregate by weekday (0=Sun..6=Sat) x hour (0-23) over last 7 days
    List<PunchcardCell> punchcard(Instant from, Instant to, String applicationId, String environment);

    record PunchcardCell(int weekday, int hour, long totalCount, long failedCount) {}
}
