"""
Environment detection for worker execution.

Detects if running in TTY (interactive) or non-TTY (container) environment.
Used to conditionally enable/disable Rich Progress displays.
"""

import sys
import os


def is_tty_environment() -> bool:
    """
    Detect if running in TTY environment.

    Returns:
        True if TTY available (interactive), False otherwise (container)
    """
    # Check if stdout is a TTY
    if not sys.stdout.isatty():
        return False

    # Check for common container/worker environment variables
    container_indicators = [
        "AWS_EXECUTION_ENV",  # AWS Lambda/ECS
        "ECS_CONTAINER_METADATA_URI",  # ECS
        "KUBERNETES_SERVICE_HOST",  # Kubernetes
        "DOCKER_CONTAINER",  # Docker
    ]

    for indicator in container_indicators:
        if os.getenv(indicator):
            return False

    return True


def should_use_progress_display() -> bool:
    """
    Determine if Rich Progress display should be used.

    Returns:
        True if progress display safe to use, False otherwise
    """
    # Can be overridden via environment variable
    force_disable = os.getenv("CZBG_DISABLE_PROGRESS", "").lower() in ("1", "true", "yes")
    if force_disable:
        return False

    force_enable = os.getenv("CZBG_ENABLE_PROGRESS", "").lower() in ("1", "true", "yes")
    if force_enable:
        return True

    # Default: use TTY detection
    return is_tty_environment()
