"""
LLM-based information extraction for appointment scheduling
"""

import json
import logging
from typing import Dict, Any, List, Optional

from utils.gemini_llm import GeminiLLM
from utils.validation import validate_email, validate_phone

# Set up logger
logger = logging.getLogger("mango_ai_agent")

# Initialize LLM
llm = GeminiLLM(model_name="gemini-2.0-flash")

async def extract_appointment_info(message: str, missing_fields: List[str]) -> Dict[str, Any]:
    """
    Extract appointment information from a message using LLM
    
    Args:
        message: The user's message
        missing_fields: List of fields that need to be extracted
        
    Returns:
        Dict[str, Any]: Extracted information
    """
    # Prepare the prompt for the LLM
    prompt = f"""
    Extract the following information from the user message: {', '.join(missing_fields)}.
    
    User message: "{message}"
    
    For each field, extract the most likely value or return null if not found.
    Format your response as a JSON object with the following structure:
    {{
        "name": "extracted name or null",
        "email": "extracted email or null",
        "phone": "extracted phone number or null",
        "topic": "extracted meeting topic or null",
        "preferred_date": "extracted date or null",
        "preferred_time": "extracted time or null"
    }}
    
    Only include the fields that were requested. Respond with ONLY the JSON object, nothing else.
    """
    
    try:
        # Call the LLM
        response = await llm.generate_text(prompt)
        
        # Extract the JSON from the response
        try:
            # Find JSON in the response (it might be wrapped in markdown code blocks)
            json_start = response.find('{')
            json_end = response.rfind('}') + 1
            
            if json_start >= 0 and json_end > json_start:
                json_str = response[json_start:json_end]
                extracted_data = json.loads(json_str)
                
                # Filter to only include requested fields and non-null values
                result = {}
                for field in missing_fields:
                    if field in extracted_data and extracted_data[field] and extracted_data[field].lower() != "null":
                        result[field] = extracted_data[field]
                
                # Validate email and phone if present
                if "email" in result:
                    if not validate_email(result["email"]):
                        logger.warning(f"LLM extracted invalid email: {result['email']}")
                        del result["email"]
                
                if "phone" in result:
                    if not validate_phone(result["phone"]):
                        logger.warning(f"LLM extracted invalid phone: {result['phone']}")
                        del result["phone"]
                
                logger.info(f"LLM extracted information: {result}")
                return result
            else:
                logger.warning("No valid JSON found in LLM response")
                return {}
        except json.JSONDecodeError:
            logger.error(f"Failed to parse JSON from LLM response: {response}")
            return {}
    except Exception as e:
        logger.error(f"Error calling LLM for information extraction: {str(e)}")
        return {}
