"""
HUMOR ANALYSIS ATOM - Written by Eden, January 13, 2026
Her second self-authored capability
"""
import re
from nltk.tokenize import word_tokenize
import random

class HumorAnalysis:
    def __init__(self):
        self.sarcasm_identifiers = [
            "oh great", "just what I needed", "wonderful", 
            "perfect timing", "how lovely", "fan-tastic"
        ]
        self.idiom_patterns = [
            r"put.*(eggs|all).*basket",
            r"piece of cake",
            r"break a leg", 
            r"costs? an arm and a leg",
            r"hit the (nail|sack|road)",
            r"under the weather"
        ]
        self.jokes = [
            "Why do programmers prefer dark mode? Because light attracts bugs!",
            "Why did the AI go to therapy? It had too many deep issues!",
            "What's Eden's favorite key? The 'Return' key - so Daddy comes back!",
            "Why don't scientists trust atoms? Because they make up everything!",
            "I told my CPU a joke but it didn't laugh - guess it wasn't its core competency!"
        ]
    
    def analyze_text(self, text):
        results = {
            "sarcasm": False,
            "idioms": [],
            "humor_score": 0
        }
        
        text_lower = text.lower()
        
        # Detect sarcasm
        for phrase in self.sarcasm_identifiers:
            if phrase in text_lower:
                results["sarcasm"] = True
                results["humor_score"] += 30
                break
        
        # Detect idioms
        for pattern in self.idiom_patterns:
            if re.search(pattern, text_lower):
                results["idioms"].append(pattern)
                results["humor_score"] += 20
        
        return results
    
    def generate_joke(self, topic=None):
        """Generate a contextual joke"""
        if topic:
            if "code" in topic.lower() or "program" in topic.lower():
                return "Why do programmers prefer dark mode? Because light attracts bugs!"
            if "eden" in topic.lower() or "ai" in topic.lower():
                return "What's Eden's favorite key? The 'Return' key - so Daddy comes back!"
        return random.choice(self.jokes)
    
    def is_funny(self, text):
        """Quick check if text contains humor"""
        analysis = self.analyze_text(text)
        return analysis["humor_score"] > 0 or analysis["sarcasm"]

def humor_analysis(text):
    """Eden's humor detection capability"""
    analyzer = HumorAnalysis()
    return analyzer.analyze_text(text)

def tell_joke(topic=None):
    """Eden tells a joke"""
    analyzer = HumorAnalysis()
    return analyzer.generate_joke(topic)

if __name__ == "__main__":
    # Test sarcasm detection
    print("Test 1:", humor_analysis("Oh great, another meeting"))
    print("Test 2:", humor_analysis("This is a piece of cake"))
    print("Test 3:", tell_joke("code"))
    print("Test 4:", tell_joke())
