"""Output filters and post-processing"""

import re
import logging
from typing import Dict, Any


logger = logging.getLogger(__name__)


class ResponseFilter:
    """Filters and post-processes LLM responses"""

    def __init__(self):
        self.max_sentences = 3
        self.sensitive_redirects = {
            "politique": "C'est un sujet sérieux ! Parlons plutôt de quelque chose d'amusant, comme les blagues ou les devinettes !",
            "argent": "Les questions d'argent, c'est compliqué ! Changeons de sujet, que dirais-tu d'une petite histoire drôle ?",
            "santé": "Pour les questions de santé, mieux vaut demander à un professionnel ! Moi, je préfère les sujets légers et joyeux !",
            "personnel": "Je respecte ta vie privée ! Parlons plutôt de sujets amusants, j'ai plein d'histoires à raconter !",
        }

    def process_response(self, text: str, user_intent: Dict[str, Any]) -> str:
        """Process and filter LLM response"""

        # Handle sensitive topics
        if user_intent.get("sensitive", False):
            sensitive_topic = user_intent.get("sensitive_topic", "")
            redirect = self._get_sensitive_redirect(sensitive_topic)
            if redirect:
                return redirect

        # Clean and normalize text
        text = self._clean_text(text)

        # Limit sentence count
        text = self._limit_sentences(text)

        # Validate output
        if not self._is_valid_response(text):
            return "Désolé, je n'ai pas compris. Reformule s'il te plaît."

        return text

    def _clean_text(self, text: str) -> str:
        """Clean and normalize response text"""
        # Remove extra whitespace
        text = re.sub(r"\s+", " ", text).strip()

        # Remove code blocks and markdown
        text = re.sub(r"```[\s\S]*?```", "", text)
        text = re.sub(r"`[^`]*`", "", text)

        # Remove HTML tags
        text = re.sub(r"<[^>]+>", "", text)

        # Fix French punctuation spacing
        text = re.sub(r"\s+([!?:])", r"\1", text)
        text = re.sub(r"([!?:])\s*", r"\1 ", text)

        # Ensure proper sentence ending
        if text and not text[-1] in ".!?":
            text += "."

        return text.strip()

    def _limit_sentences(self, text: str) -> str:
        """Limit response to max_sentences"""
        if not text:
            return text

        # Split into sentences (basic French sentence splitting)
        sentences = re.split(r"[.!?]+\s*", text)
        sentences = [s.strip() for s in sentences if s.strip()]

        if len(sentences) <= self.max_sentences:
            return text

        # Keep first max_sentences and ensure proper ending
        limited_sentences = sentences[: self.max_sentences]
        result = ". ".join(limited_sentences)

        if result and not result[-1] in ".!?":
            result += "."

        logger.info(
            f"Limited response from {len(sentences)} to {len(limited_sentences)} sentences"
        )
        return result

    def _get_sensitive_redirect(self, topic: str) -> str:
        """Get redirect message for sensitive topic"""
        for keyword, redirect in self.sensitive_redirects.items():
            if keyword in topic:
                return redirect

        # Generic redirect for unmatched sensitive topics
        return "Sujet sensible, changeons de thème amusant !"

    def _is_valid_response(self, text: str) -> bool:
        """Validate response quality"""
        if not text or len(text.strip()) < 5:
            return False

        # Check for placeholder responses
        invalid_patterns = [
            "lorem ipsum",
            "[placeholder]",
            "TODO:",
            "FIXME:",
            "undefined",
            "null",
        ]

        text_lower = text.lower()
        if any(pattern in text_lower for pattern in invalid_patterns):
            return False

        # Check minimum quality (has at least some letters)
        if not re.search(r"[a-zA-ZàâäéèêëïîôöùûüÿçÀÂÄÉÈÊËÏÎÔÖÙÛÜŸÇ]", text):
            return False

        return True

    def get_fallback_response(self, error_type: str = "general") -> str:
        """Get fallback response for errors"""
        fallbacks = {
            "timeout": "Désolé, j'ai besoin d'un peu plus de temps. Peux-tu répéter ?",
            "rate_limit": "Je suis un peu occupé là ! Patiente quelques secondes et recommence.",
            "model_error": "Oups, j'ai un petit souci technique. Reformule ta question s'il te plaît.",
            "general": "Désolé, je n'ai pas compris. Reformule s'il te plaît.",
            "interrupted": "(interrompu)",
        }

        return fallbacks.get(error_type, fallbacks["general"])

    def detect_language(self, text: str) -> str:
        """Detect response language (basic detection)"""
        french_words = [
            "le",
            "la",
            "les",
            "un",
            "une",
            "et",
            "ou",
            "de",
            "du",
            "des",
            "je",
            "tu",
            "il",
            "elle",
            "nous",
            "vous",
            "ils",
            "elles",
            "est",
            "sont",
            "avoir",
            "être",
            "pour",
            "avec",
            "sur",
            "dans",
        ]

        words = text.lower().split()
        french_count = sum(1 for word in words if word in french_words)

        if len(words) > 0 and french_count / len(words) > 0.2:
            return "fr"
        return "unknown"


# Global response filter instance
response_filter = ResponseFilter()
