"""
Stage utility functions - Common utilities for stage processing.

This module contains utility functions that are used across multiple stage processing
components, extracted to avoid circular import issues.
"""

from pathlib import Path
from typing import (
    Any,
    Dict,
    List,
    Union,
)

# Simple replacement for legacy pipeline import
def get_config_section(stage_name: str) -> str:
    """
    Simple replacement for legacy get_config_section.
    Maps stage names to their configuration section names.
    """
    stage_to_config_mapping = {
        "generate": "generate",
        "k8s": "k8s", 
        "metadata": "metadata",
        "usage": "usage",
        "discount": "discount",
        "tax": "tax"
    }
    return stage_to_config_mapping.get(stage_name, stage_name)


def create_stage_validator(stage_name: str, run_dir: Path, data_manager=None):
    """
    Create a stage-specific validator for the given stage.

    NOTE: Complex validation orchestration has been removed in favor of
    essential inline validation during immediate write processing.
    This function now returns None to indicate validation is handled
    during data generation instead of post-processing.

    Args:
        stage_name: Stage name (unused)
        run_dir: Run directory (unused)
        data_manager: DataManager instance (unused)

    Returns:
        None - validation now handled inline during immediate write
    """
    # Complex validation orchestration removed - validation is now handled
    # inline during immediate write processing in ImmediateWriter
    return None


def get_stage_config(
    config: Dict[str, Any], stage_name: str
) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
    """
    Get configuration for a specific stage.

    Args:
        config: Full pipeline configuration
        stage_name: Stage name

    Returns:
        Stage-specific configuration
    """
    stages_config = config.get("stages", {})

    # Get the config section name from the registry
    config_section = get_config_section(stage_name)

    # Handle special case for generate stage (list of job configs)
    if stage_name == "generate":
        return stages_config.get(config_section, [])

    # Other stages have direct configuration
    return stages_config.get(config_section, {})
