"""
Time-shift result aggregation service.

Aggregates results from multiple provider time-shifts.
"""

from typing import List
from ..models.parallel_execution_models import ProviderResult, AggregateResult


class TimeShiftResultAggregator:
    """Aggregates results from multiple time-shift jobs."""

    def aggregate(
        self,
        provider_results: List[ProviderResult]
    ) -> AggregateResult:
        """
        Aggregate provider results into summary.

        Args:
            provider_results: List of results from individual providers

        Returns:
            AggregateResult with totals across all providers
        """
        return AggregateResult(
            total_drops=self._sum_total_drops(provider_results),
            drops_completed=self._sum_completed_drops(provider_results),
            drops_failed=self._sum_failed_drops(provider_results),
            total_records=self._sum_records(provider_results),
            total_time=self._max_time(provider_results),
            provider_results=provider_results
        )

    def _sum_total_drops(self, results: List[ProviderResult]) -> int:
        """Sum total drops across all providers."""
        return sum(r.drops_total for r in results)

    def _sum_completed_drops(self, results: List[ProviderResult]) -> int:
        """Sum completed drops across all providers."""
        return sum(r.drops_completed for r in results)

    def _sum_failed_drops(self, results: List[ProviderResult]) -> int:
        """Calculate total failed drops."""
        return sum(r.drops_total - r.drops_completed for r in results)

    def _sum_records(self, results: List[ProviderResult]) -> int:
        """Sum records processed across all providers."""
        return sum(r.records_processed for r in results)

    def _max_time(self, results: List[ProviderResult]) -> float:
        """Get maximum processing time (longest provider)."""
        return max(r.time_elapsed for r in results) if results else 0.0
