"""
Smart skip decider for Mode 2 upload logic.

Determines when data processing can be safely skipped to save time and costs.
"""

from typing import Optional, Tuple
from .data_lifecycle_manager import DataLifecycleManager


class SmartSkipDecider:
    """
    Determines if processing can be skipped (Mode 2 logic).

    Single Responsibility: Skip decision logic based on data existence
    and delete_data flag.
    """

    def __init__(self, data_lifecycle: DataLifecycleManager):
        """
        Initialize skip decider with data lifecycle manager.

        Args:
            data_lifecycle: DataLifecycleManager for checking S3 data
        """
        self.data_lifecycle = data_lifecycle

    def should_skip_processing(
        self,
        org: str,
        provider: str,
        suffix: Optional[str],
        delete_data: bool
    ) -> Tuple[bool, str]:
        """
        Determine if processing can be skipped.

        Skip Logic (Mode 2):
        - delete_data=False (Mode 2)
        - AND S3 data already exists
        -> Skip processing to save time/cost

        Never Skip (Mode 3):
        - delete_data=True (Mode 3)
        -> Always process and recreate

        Args:
            org: Organization name
            provider: Provider name
            suffix: Optional connection suffix
            delete_data: Whether data will be deleted

        Returns:
            Tuple of (should_skip: bool, reason: str)
            - (True, reason) if processing can be skipped
            - (False, reason) if processing required
        """
        # Mode 3: Never skip when delete_data=True
        if delete_data:
            return (False, "Mode 3: delete_data=True, always process")

        # Mode 2: Check if data exists
        data_exists = self.data_lifecycle.check_data_exists(org, provider, suffix)

        if data_exists:
            s3_folder = self._format_s3_folder(provider, suffix)
            return (
                True,
                f"Mode 2: S3 data exists at {org}/{s3_folder}, skipping to preserve data"
            )

        # No data exists, processing required
        return (False, "S3 data not found, processing required to create data")

    def _format_s3_folder(self, provider: str, suffix: Optional[str]) -> str:
        """Format S3 folder name for display."""
        if suffix:
            return f"{provider}-{suffix}"
        return provider
