"""
Time-shift console reporting service.

Formats and prints time-shift progress and results to console.
"""

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


class TimeShiftConsoleReporter:
    """Reports time-shift progress and results to console."""

    def report_split(self, providers: List[str], execution_id: str) -> None:
        """
        Report config split by provider.

        Args:
            providers: List of provider names
            execution_id: Unique execution ID for this run
        """
        print(f"\n📊 Splitting by provider: {len(providers)} providers (execution_id: {execution_id})\n")
        for provider in providers:
            print(f"   • {provider}")

    def report_submissions_start(self, provider_count: int) -> None:
        """
        Report start of job submissions.

        Args:
            provider_count: Number of jobs being submitted
        """
        print(f"\n🚀 Submitting {provider_count} jobs to API...\n")

    def report_submission(
        self,
        provider: str,
        job_id: str,
        dataset_name: str
    ) -> None:
        """
        Report single job submission.

        Args:
            provider: Provider name
            job_id: Job ID from API
            dataset_name: Dataset name used for message_group_id
        """
        short_id = job_id[:8]
        print(f"   ✅ {provider:20} job_id={short_id}... (group={dataset_name})")

    def report_monitoring_start(self, job_count: int) -> None:
        """
        Report monitoring start.

        Args:
            job_count: Number of jobs being monitored
        """
        print(f"\n📊 Monitoring {job_count} jobs (press Ctrl+C to stop, jobs continue running)...\n")

    def report_completion(
        self,
        aggregate_result: AggregateResult
    ) -> None:
        """
        Report final aggregated results.

        Args:
            aggregate_result: Aggregated results from all providers
        """
        print(f"\n✅ All jobs complete!\n")
        print(f"   Total drops:    {aggregate_result.drops_completed:,}/{aggregate_result.total_drops:,}")
        print(f"   Total records:  {aggregate_result.total_records:,}")
        print(f"   Total time:     {aggregate_result.total_time:.1f}s ({aggregate_result.total_time/3600:.2f} hours)")

        if aggregate_result.drops_failed > 0:
            print(f"   ❌ Failed drops: {aggregate_result.drops_failed:,}")

        # Show per-provider breakdown
        print(f"\n📊 Per-Provider Results:\n")
        for result in aggregate_result.provider_results:
            status_icon = "✅" if result.status in ("completed", "success") else "❌"
            print(
                f"   {status_icon} {result.provider:20} "
                f"{result.drops_completed:,}/{result.drops_total:,} drops, "
                f"{result.records_processed:,} records, "
                f"{result.time_elapsed:.1f}s"
            )
