"""
Steps sanitization service to clean and validate generated steps.
"""
from typing import Dict, Any

from src.bot.utils.text_utils import is_bad_step
from src.bot.utils.survey_constraints import (
    is_sports_technique,
    uses_social_networks,
    get_default_step1_for_sport_no_social,
    get_default_step1_generic
)


class StepsSanitizer:
    """Service for sanitizing and cleaning generated steps."""
    
    def __init__(self):
        pass
    
    def sanitize_steps(self, raw_steps: Dict[str, Any], survey: Dict[str, Any]) -> Dict[str, Any]:
        """
        Sanitize steps by removing bad patterns and adding defaults if needed.
        
        Args:
            raw_steps: Raw steps dictionary from LLM
            survey: Survey data for context
            
        Returns:
            Sanitized steps dictionary
        """
        steps = dict(raw_steps or {})
        
        # Check first step for bad patterns
        first = steps.get("1", "")
        if isinstance(first, dict):
            first_text = first.get("html", "") or ""
        else:
            first_text = str(first or "")

        if is_bad_step(first_text):
            if is_sports_technique(survey) and not uses_social_networks(survey):
                steps["1"] = get_default_step1_for_sport_no_social(survey.get("city") or "")
            else:
                steps["1"] = get_default_step1_generic()
        
        return steps
