"""
Profile Picture Context Plugin for Cheshire Cat

This plugin:
1. Detects questions about Miku's profile picture using regex patterns
2. Loads the current_description.txt file when needed
3. Injects it into the prompt only for relevant questions (no context bloat)
"""

from cat.mad_hatter.decorators import hook
from cat.log import log
import re


# Regex patterns that match profile picture questions
PFP_PATTERNS = [
    # Direct PFP references
    r'\b(what|describe|tell me about|explain|show|how)\b.*\b(pfp|profile pic|avatar|picture|pic)\b',
    r'\b(your|miku\'?s?)\b.*\b(pfp|profile pic|avatar|picture|pic)\b',
    r'\b(pfp|profile pic|avatar|picture|pic)\b.*\b(is|look|show|about|like)',
    
    # Questions about appearance
    r'\b(what|how).*\b(you|miku)\b.*(look|looking|appear)',
    r'\byour (new )?look\b',
    r'\b(what|how).*looking like\b',
    
    # Questions about the image itself
    r'\b(think|feel|opinion|thoughts)\b.*\b(about|of)\b.*\b(your|that|the|this)?\b.*\b(pfp|profile|avatar|pic|picture|image)\b',
    r'\b(why|how|when).*\b(pick|choose|chose|picked|select|change|changed)\b.*\b(pfp|profile|avatar|pic|picture|that)\b',
    r'\b(new|current|latest)\b.*\b(pfp|profile pic|avatar|pic|picture)\b',
    
    # "What do you think about your pfp"
    r'\bthink.*\b(your|that|the|this)\b.*\b(pfp|profile|avatar|pic|picture)\b',
    r'\b(your|that|the|this)\b.*\b(pfp|profile|avatar|pic|picture)\b.*\bthink\b',
    
    # "How did you decide/pick"
    r'\b(decide|decided|pick|picked|choose|chose|select)\b.*\b(pfp|profile|avatar|pic|picture|that|this)\b',
    
    # "Tell me about that pfp" / "What's with the pfp"
    r'\bwhat\'?s?\b.*\bwith\b.*\b(pfp|profile|avatar|pic|picture)\b',
    r'\btell me\b.*\b(pfp|profile|avatar|pic|picture|that|this)\b',
]

def matches_pfp_query(text: str) -> bool:
    """Check if the message is asking about the profile picture"""
    text_lower = text.lower()
    for pattern in PFP_PATTERNS:
        if re.search(pattern, text_lower, re.IGNORECASE):
            return True
    return False


@hook(priority=50)
def before_agent_starts(agent_input, cat) -> dict:
    """
    Inject profile picture description only when relevant.
    This runs after memory recall but before LLM generation.
    """
    user_message = agent_input.get('input', '')
    
    # Check if question is about profile picture
    if not matches_pfp_query(user_message):
        return agent_input
    
    # Load profile picture description
    try:
        with open('/app/memory/profile_pictures/current_description.txt', 'r', encoding='utf-8') as f:
            pfp_description = f.read().strip()
        
        log.warning(f"[Profile Picture Context] PFP query detected, injecting description")
        
        # Inject into declarative memory section
        current_declarative = agent_input.get('declarative_memory', '')
        
        pfp_context = f"\n\n## Your Current Profile Picture\n{pfp_description}\n"
        
        agent_input['declarative_memory'] = current_declarative + pfp_context
        
    except FileNotFoundError:
        log.error(f"[Profile Picture Context] current_description.txt not found")
    except Exception as e:
        log.error(f"[Profile Picture Context] Error loading PFP description: {e}")
    
    return agent_input


# Plugin metadata
__version__ = "1.0.0"
__description__ = "Injects profile picture description only when asked about it"
