"""
Progress tracking abstraction for K8s month processing.

Single Responsibility: Abstract progress tracking UI from business logic.
"""

import sys
from typing import Protocol, Any
from rich.console import Console
from rich.progress import Progress, TaskID


class ProgressTracker(Protocol):
    """
    Protocol for progress tracking.

    Allows business logic to be independent of UI implementation.
    """

    def start(self, total: int, description: str) -> None:
        """
        Start progress tracking.

        Args:
            total: Total number of items to process
            description: Description of the task
        """
        ...

    def update(self, description: str | None = None, advance: int = 0) -> None:
        """
        Update progress.

        Args:
            description: Optional new description
            advance: Number of items completed (default 0)
        """
        ...

    def finish(self) -> None:
        """Finish progress tracking and cleanup."""
        ...


class RichProgressTracker:
    """
    Rich Progress implementation for TTY mode.

    Uses Rich library for interactive progress display.
    Max 5 methods per CLAUDE.md guidelines.
    """

    def __init__(self, console: Console):
        """
        Initialize Rich progress tracker.

        Args:
            console: Rich console instance
        """
        self.console = console
        self.progress: Progress | None = None
        self.task: TaskID | None = None

    def start(self, total: int, description: str) -> None:
        """Start Rich progress bar."""
        self.progress = Progress(console=self.console)
        self.progress.start()
        self.task = self.progress.add_task(description, total=total)

    def update(self, description: str | None = None, advance: int = 0) -> None:
        """Update Rich progress bar."""
        if self.progress and self.task is not None:
            kwargs = {"advance": advance}
            if description:
                kwargs["description"] = description
            self.progress.update(self.task, **kwargs)

    def finish(self) -> None:
        """Stop Rich progress bar."""
        if self.progress:
            self.progress.stop()
            self.progress = None
            self.task = None


class NoOpProgressTracker:
    """
    No-op implementation for headless mode.

    Silently discards all progress updates.
    Max 5 methods per CLAUDE.md guidelines.
    """

    def start(self, total: int, description: str) -> None:
        """No-op start (headless mode)."""
        pass

    def update(self, description: str | None = None, advance: int = 0) -> None:
        """No-op update (headless mode)."""
        pass

    def finish(self) -> None:
        """No-op finish (headless mode)."""
        pass


def create_progress_tracker(console: Console) -> ProgressTracker:
    """
    Factory to create appropriate progress tracker.

    Args:
        console: Rich console instance

    Returns:
        RichProgressTracker for TTY, NoOpProgressTracker for headless
    """
    if sys.stdout.isatty():
        return RichProgressTracker(console)
    return NoOpProgressTracker()
