"""
AI Chat Service - Handles AI-powered chat responses
Integrates with OpenAI, Anthropic, or other LLM providers
"""

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

from ..services.ai_assistant_service import AIAssistantService
from ..services.ai_knowledge_base import AIKnowledgeBase
from ..services.ai_role_access_service import AIRoleAccessService
from ..services.ai_learning_service import AILearningService
from ...auth.models.user import User
from ..models.ai_conversation import AILearnedPattern
from ...shared.config.logging_config import get_logger

logger = get_logger(__name__)


class AIChatService:
    """AI Chat Service for generating intelligent responses"""
    
    def __init__(self, db_session, current_user: Optional[User] = None):
        self.db = db_session
        self.current_user = current_user
        self.ai_assistant = AIAssistantService(db_session, current_user)
        self.knowledge_base = AIKnowledgeBase()
        self.role_access_service = AIRoleAccessService(db_session, current_user) if current_user else None
        self.learning_service = AILearningService(db_session)
        # Configure AI provider (OpenAI, Anthropic, etc.)
        self.provider = os.getenv("AI_PROVIDER", "openai")
        self.api_key = os.getenv("AI_API_KEY", "")
        self.model = os.getenv("AI_MODEL", "gpt-4")
    
    def generate_response(self, user_query: str, context: Optional[Dict] = None, session_id: Optional[str] = None) -> Dict[str, Any]:
        """
        Generate AI response based on user query with self-learning
        """
        start_time = datetime.utcnow()
        
        try:
            # Check learned patterns first
            role_name = self.role_access_service.role_name if self.role_access_service else None
            learned_pattern = self.learning_service.get_learned_pattern(user_query, role_name)
            knowledge_entry = self.learning_service.get_knowledge_entry(user_query, role_name)
            
            # Check for similar past queries
            similar_queries = self.learning_service.find_similar_queries(user_query, limit=3)
            
            # Get current system context
            system_context = self.ai_assistant.generate_context_for_ai()
            
            # Merge with provided context
            if context:
                system_context.update(context)
            
            # Add learned knowledge to context
            if learned_pattern:
                system_context["learned_pattern"] = {
                    "pattern": learned_pattern.query_pattern,
                    "intent": learned_pattern.intent,
                    "confidence": float(learned_pattern.confidence_score)
                }
            
            if knowledge_entry:
                system_context["knowledge_entry"] = knowledge_entry
            
            if similar_queries:
                system_context["similar_past_queries"] = similar_queries
            
            # Analyze query intent (or use learned intent)
            intent = learned_pattern.intent if learned_pattern else self._analyze_intent(user_query)
            
            # Get relevant data based on intent
            relevant_data = self._get_relevant_data(intent, user_query)
            
            # Status-related intents should always use rule-based formatted responses
            # These provide consistent, well-formatted output with emojis and structure
            status_intents = [
                "general_status", "room_status", "room_occupied", "room_available", 
                "room_problems", "booking_status", "payment_status", "invoice_status",
                "chat_status", "app_info"
            ]
            
            response = None
            
            # For status queries, prioritize rule-based formatted responses
            if intent in status_intents:
                # Always use rule-based response for status queries to ensure proper formatting
                if self.api_key and self.provider == "openai":
                    # Even with LLM, prefer rule-based for status to maintain format consistency
                    response = self._generate_rule_based_response(intent, user_query, relevant_data)
                else:
                    response = self._generate_rule_based_response(intent, user_query, relevant_data)
                logger.info(f"Using rule-based formatted response for status intent: {intent}")
            else:
                # For non-status queries, use learned patterns/knowledge if available
                if learned_pattern and learned_pattern.confidence_score >= 70.0 and learned_pattern.response_template:
                    # Try to use learned pattern response
                    try:
                        response = self._apply_learned_pattern(learned_pattern, relevant_data, user_query)
                        if response:
                            logger.info(f"Using learned pattern {learned_pattern.id} for query")
                    except Exception as e:
                        logger.warning(f"Failed to apply learned pattern: {str(e)}")
                
                # Use knowledge entry if available
                if not response and knowledge_entry and knowledge_entry.get("confidence", 0) >= 70.0:
                    response = knowledge_entry.get("answer")
                    logger.info(f"Using knowledge entry for query")
                
                # Generate response using LLM or rule-based system if no learned response
                if not response:
                    if self.api_key and self.provider == "openai":
                        response = self._generate_llm_response(user_query, system_context, relevant_data)
                    else:
                        # Fallback to rule-based responses
                        response = self._generate_rule_based_response(intent, user_query, relevant_data)
            
            # Calculate response time
            response_time = int((datetime.utcnow() - start_time).total_seconds() * 1000)
            
            # Save conversation for learning (async, don't block response)
            if self.current_user:
                try:
                    self.learning_service.save_conversation(
                        user_id=self.current_user.id,
                        user_query=user_query,
                        ai_response=response,
                        intent=intent,
                        context_used=relevant_data,
                        user_role=role_name,
                        session_id=session_id,
                        response_time_ms=response_time
                    )
                except Exception as e:
                    logger.warning(f"Failed to save conversation for learning: {str(e)}")
            
            return {
                "response": response,
                "intent": intent,
                "data_used": relevant_data,
                "timestamp": datetime.utcnow().isoformat(),
                "used_learned_pattern": learned_pattern.id if learned_pattern else None,
                "used_knowledge_entry": knowledge_entry.get("topic") if knowledge_entry else None
            }
        except Exception as e:
            logger.error(f"Error generating AI response: {str(e)}", exc_info=True)
            return {
                "response": "I apologize, but I'm having trouble processing your request right now. Please try again later.",
                "error": str(e)
            }
    
    def _analyze_intent(self, query: str) -> str:
        """Analyze user query intent with comprehensive pattern matching"""
        query_lower = query.lower()
        
        # Room-related queries - expanded patterns
        room_keywords = ['room', 'rooms', 'occupied', 'available', 'free', 'vacant', 'vacancy', 
                        'occupancy', 'suite', 'suites', 'accommodation']
        if any(word in query_lower for word in room_keywords):
            if any(word in query_lower for word in ['occupied', 'booked', 'taken', 'in use', 'guest']):
                return "room_occupied"
            elif any(word in query_lower for word in ['available', 'free', 'vacant', 'empty', 'ready']):
                return "room_available"
            elif any(word in query_lower for word in ['problem', 'issue', 'maintenance', 'broken', 'repair', 'fix']):
                return "room_problems"
            elif any(word in query_lower for word in ['how many', 'total', 'count', 'number of', 'how much', 'quantity']):
                return "room_status"
            elif any(word in query_lower for word in ['status', 'state', 'condition', 'overview']):
                return "room_status"
            return "room_status"
        
        # Booking-related queries - expanded patterns
        booking_keywords = ['booking', 'bookings', 'reservation', 'reservations', 'check-in', 'check-out', 
                           'checkin', 'checkout', 'arrival', 'departure', 'guest', 'guests']
        if any(word in query_lower for word in booking_keywords):
            if any(word in query_lower for word in ['number', 'id', 'code', 'reference']):
                return "booking_by_number"
            elif any(word in query_lower for word in ['upcoming', 'coming', 'next', 'future', 'scheduled']):
                return "booking_status"
            elif any(word in query_lower for word in ['active', 'current', 'today', 'now']):
                return "booking_status"
            return "booking_status"
        
        # Invoice-related queries - expanded patterns
        invoice_keywords = ['invoice', 'invoices', 'bill', 'bills', 'receipt', 'receipts', 'statement', 'statements']
        if any(word in query_lower for word in invoice_keywords):
            if any(word in query_lower for word in ['number', 'id', 'code', 'reference']):
                return "invoice_by_number"
            elif any(word in query_lower for word in ['overdue', 'unpaid', 'pending', 'due']):
                return "invoice_status"
            return "invoice_status"
        
        # Payment-related queries - expanded patterns
        payment_keywords = ['payment', 'payments', 'paid', 'unpaid', 'revenue', 'money', 'income', 
                           'earnings', 'sales', 'financial', 'cash', 'transaction', 'transactions']
        if any(word in query_lower for word in payment_keywords):
            if any(word in query_lower for word in ['all', 'list', 'show all', 'every', 'entire']):
                return "all_payments"
            return "payment_status"
        
        # Chat-related queries - expanded patterns
        chat_keywords = ['chat', 'chats', 'message', 'messages', 'unanswered', 'customer', 'customers', 
                        'inquiry', 'inquiries', 'support', 'help', 'contact']
        if any(word in query_lower for word in chat_keywords):
            return "chat_status"
        
        # App/Platform information queries - for admin only
        app_keywords = ['app', 'application', 'platform', 'system', 'software', 'what is this', 
                       'about the app', 'about the platform', 'about the system', 'features',
                       'capabilities', 'what can', 'how does the app', 'tell me about the app',
                       'what does this app do', 'app features', 'system features', 'platform features',
                       'what features', 'list features', 'show features', 'tell me about features',
                       'what modules', 'what models', 'system overview', 'platform overview']
        if any(word in query_lower for word in app_keywords):
            return "app_info"
        
        # General status queries - expanded patterns
        status_keywords = ['status', 'summary', 'overview', 'dashboard', 'report', 'reports', 
                          'statistics', 'stats', 'info', 'information', 'details', 'what', 'show me', 'tell me']
        if any(word in query_lower for word in status_keywords):
            return "general_status"
        
        # Questions about "how many", "what", "show", "tell" - default to general status
        question_words = ['how many', 'how much', 'what', 'show', 'tell', 'give me', 'list', 'display']
        if any(word in query_lower for word in question_words):
            return "general_status"
        
        return "general_query"
    
    def _get_relevant_data(self, intent: str, query: str) -> Dict[str, Any]:
        """Get relevant data based on intent with role-based filtering"""
        data = {}
        role_name = self.role_access_service.role_name if self.role_access_service else "customer"
        
        try:
            if intent == "room_occupied":
                # Only staff/admin can see occupied room details
                if role_name in ["admin", "staff"]:
                    room_number = self._extract_room_number(query)
                    data["occupied_rooms"] = self.ai_assistant.search_occupied_rooms(room_number)
                data["room_summary"] = self.ai_assistant.get_room_status_summary()
            
            elif intent == "room_available":
                data["room_summary"] = self.ai_assistant.get_room_status_summary()
            
            elif intent == "room_status":
                data["room_summary"] = self.ai_assistant.get_room_status_summary()
            
            elif intent == "room_problems":
                # Only staff/admin can see room problems
                if role_name in ["admin", "staff"]:
                    data["problems"] = self.ai_assistant.get_room_problems()
            
            elif intent == "booking_by_number":
                booking_number = self._extract_booking_number(query)
                if booking_number:
                    booking_data = self.ai_assistant.get_booking_by_number(booking_number)
                    if booking_data:
                        # Filter based on role
                        if role_name == "customer":
                            # Customers can only see their own bookings
                            user_bookings = self.role_access_service.get_user_bookings(limit=100)
                            if any(b.get("booking_number") == booking_number for b in user_bookings):
                                data["booking"] = booking_data
                        else:
                            data["booking"] = booking_data
            
            elif intent == "booking_status":
                if role_name in ["admin", "staff"]:
                    data["booking_summary"] = self.ai_assistant.get_booking_summary()
                elif role_name == "customer":
                    # Customers see their own bookings
                    data["user_bookings"] = self.role_access_service.get_user_bookings(limit=10)
            
            elif intent == "invoice_by_number":
                invoice_number = self._extract_invoice_number(query)
                if invoice_number:
                    invoice_data = self.ai_assistant.get_invoice_by_number(invoice_number)
                    if invoice_data:
                        # Filter based on role
                        if role_name == "customer":
                            user_invoices = self.role_access_service.get_user_invoices(limit=100)
                            if any(i.get("invoice_number") == invoice_number for i in user_invoices):
                                data["invoice"] = invoice_data
                        else:
                            data["invoice"] = invoice_data
            
            elif intent == "invoice_status":
                if role_name in ["admin", "accountant", "staff"]:
                    invoice_summary = self.role_access_service.get_invoice_summary()
                    if invoice_summary:
                        data["invoice_summary"] = invoice_summary
                elif role_name == "customer":
                    # Customers see their own invoices
                    data["user_invoices"] = self.role_access_service.get_user_invoices(limit=10)
            
            elif intent == "all_payments":
                # Get all payments for admin/accountant
                if role_name in ["admin", "accountant"]:
                    data["all_payments"] = self.role_access_service.get_all_payments()
                elif role_name == "staff":
                    data["all_payments"] = self.role_access_service.get_user_payments(limit=50)
                elif role_name == "customer":
                    data["all_payments"] = self.role_access_service.get_user_payments(limit=None)
            
            elif intent == "payment_status":
                if role_name in ["admin", "accountant", "staff"]:
                    payment_summary = self.role_access_service.get_payment_summary()
                    if payment_summary:
                        data["payment_summary"] = payment_summary
                elif role_name == "customer":
                    # Customers see their own payments
                    data["user_payments"] = self.role_access_service.get_user_payments(limit=10)
            
            elif intent == "chat_status":
                # Only staff/admin can see unanswered chats
                if role_name in ["admin", "staff"]:
                    data["unanswered_chats"] = self.ai_assistant.get_unanswered_chats()
            
            elif intent == "general_status":
                data["room_summary"] = self.ai_assistant.get_room_status_summary()
                
                if role_name in ["admin", "staff"]:
                    data["booking_summary"] = self.ai_assistant.get_booking_summary()
                    data["unanswered_chats"] = self.ai_assistant.get_unanswered_chats()
                    data["room_problems"] = self.ai_assistant.get_room_problems()
                elif role_name == "customer":
                    data["user_bookings"] = self.role_access_service.get_user_bookings(limit=5)
                    data["user_invoices"] = self.role_access_service.get_user_invoices(limit=5)
                    data["user_payments"] = self.role_access_service.get_user_payments(limit=5)
                
                # Financial summaries only for authorized roles
                if role_name in ["admin", "accountant", "staff"]:
                    payment_summary = self.role_access_service.get_payment_summary()
                    if payment_summary:
                        data["payment_summary"] = payment_summary
                    
                    invoice_summary = self.role_access_service.get_invoice_summary()
                    if invoice_summary:
                        data["invoice_summary"] = invoice_summary
            
            elif intent == "app_info":
                # App information - only for admin
                if role_name == "admin":
                    # Get comprehensive application knowledge
                    app_knowledge = self.knowledge_base.get_application_knowledge()
                    data["app_knowledge"] = app_knowledge
                else:
                    # Non-admin users get limited information
                    data["app_knowledge"] = {
                        "overview": self.knowledge_base.APPLICATION_OVERVIEW,
                        "role_info": self.knowledge_base.get_role_info(role_name)
                    }
            
            elif intent == "general_query":
                # For general queries, get role-appropriate data
                data["room_summary"] = self.ai_assistant.get_room_status_summary()
                
                if role_name in ["admin", "staff"]:
                    data["booking_summary"] = self.ai_assistant.get_booking_summary()
                elif role_name == "customer":
                    data["user_bookings"] = self.role_access_service.get_user_bookings(limit=5)
                
                if role_name in ["admin", "accountant"]:
                    payment_summary = self.role_access_service.get_payment_summary()
                    if payment_summary:
                        data["payment_summary"] = payment_summary
                    
                    invoice_summary = self.role_access_service.get_invoice_summary()
                    if invoice_summary:
                        data["invoice_summary"] = invoice_summary
        
        except Exception as e:
            logger.error(f"Error getting relevant data: {str(e)}", exc_info=True)
        
        return data
    
    def _extract_room_number(self, query: str) -> Optional[str]:
        """Extract room number from query"""
        import re
        # Look for patterns like "room 101", "room101", "#101"
        patterns = [
            r'room\s*#?\s*(\d+)',
            r'#\s*(\d+)',
            r'room\s+(\d{3,4})',
        ]
        for pattern in patterns:
            match = re.search(pattern, query, re.IGNORECASE)
            if match:
                return match.group(1)
        return None
    
    def _extract_booking_number(self, query: str) -> Optional[str]:
        """Extract booking number from query"""
        import re
        # Look for booking number patterns
        patterns = [
            r'booking\s*#?\s*([A-Z0-9-]+)',
            r'BK[A-Z0-9-]+',
        ]
        for pattern in patterns:
            match = re.search(pattern, query, re.IGNORECASE)
            if match:
                return match.group(1) if match.groups() else match.group(0)
        return None
    
    def _extract_invoice_number(self, query: str) -> Optional[str]:
        """Extract invoice number from query"""
        import re
        patterns = [
            r'invoice\s*#?\s*([A-Z0-9-]+)',
            r'INV[A-Z0-9-]+',
        ]
        for pattern in patterns:
            match = re.search(pattern, query, re.IGNORECASE)
            if match:
                return match.group(1) if match.groups() else match.group(0)
        return None
    
    def _generate_llm_response(self, query: str, context: Dict, data: Dict) -> str:
        """Generate response using LLM (OpenAI, Anthropic, etc.)"""
        try:
            if self.provider == "openai":
                import openai
                openai.api_key = self.api_key
                
                # Build system prompt
                system_prompt = self._build_system_prompt(context, data)
                
                # Call OpenAI API
                response = openai.ChatCompletion.create(
                    model=self.model,
                    messages=[
                        {"role": "system", "content": system_prompt},
                        {"role": "user", "content": query}
                    ],
                    temperature=0.7,
                    max_tokens=500
                )
                
                return response.choices[0].message.content.strip()
        
        except ImportError:
            logger.warning("OpenAI library not installed, falling back to rule-based responses")
            return self._generate_rule_based_response(self._analyze_intent(query), query, data)
        except Exception as e:
            logger.error(f"Error calling LLM: {str(e)}", exc_info=True)
            return self._generate_rule_based_response(self._analyze_intent(query), query, data)
    
    def _build_system_prompt(self, context: Dict, data: Dict) -> str:
        """Build enterprise-level system prompt for LLM with role awareness"""
        role_name = context.get("user_role", "customer")
        role_info = self.knowledge_base.get_role_info(role_name)
        
        prompt = f"""You are an enterprise AI assistant for a comprehensive Hotel Booking and Management System (HBMS).
You provide intelligent, role-aware assistance based on the user's permissions and access level.

USER ROLE: {role_name.upper()}
ROLE DESCRIPTION: {role_info.get('description', 'Standard user access')}

YOUR CAPABILITIES:
"""
        
        # Add role-specific capabilities
        can_access = role_info.get("can_access", [])
        for capability in can_access[:15]:  # Limit to avoid token overflow
            prompt += f"- {capability}\n"
        
        if role_info.get("cannot_access"):
            prompt += "\nRESTRICTIONS:\n"
            for restriction in role_info.get("cannot_access", [])[:5]:
                prompt += f"- Cannot access: {restriction}\n"
        
        prompt += """
\nSYSTEM FEATURES AVAILABLE:
- Booking Management (create, view, modify, cancel bookings)
- Room Management (availability, status, types, amenities)
- Payment Processing (multiple payment methods, status tracking)
- Invoice Management (generation, tracking, payment reconciliation)
- Loyalty Program (points, tiers, rewards, referrals)
- Service Management (additional hotel services)
- Guest Management (profiles, preferences, CRM)
- Chat Support System
- Check-in/Check-out Operations
- Maintenance and Housekeeping

IMPORTANT SECURITY RULES:
1. NEVER provide information about other users' bookings, payments, or personal data unless user is Admin
2. For customers: Only show information about their own bookings, invoices, and payments
3. For staff: Provide operational data but not financial summaries
4. For accountant: Provide financial data but not operational details
5. For admin: Full access to all information
        
Current System Status:
"""
        prompt += json.dumps(context, indent=2, default=str)
        
        if data:
            prompt += "\n\nRelevant Data for Current Query:\n"
            prompt += json.dumps(data, indent=2, default=str)
        
        prompt += """
\nINSTRUCTIONS:
- Provide clear, concise, and helpful responses
- Use the data provided to answer questions accurately
- Respect role-based access restrictions
- If you don't have specific information or the user doesn't have access, explain clearly
- Be professional and friendly
- For customers, always refer to data as "your bookings", "your invoices", etc.
- For staff/admin, provide comprehensive operational insights
- For accountants, focus on financial data and reports"""
        
        return prompt
    
    def _generate_rule_based_response(self, intent: str, query: str, data: Dict) -> str:
        """Generate rule-based response when LLM is not available - with role awareness"""
        role_name = self.role_access_service.role_name if self.role_access_service else "customer"
        is_customer = role_name == "customer"
        
        if intent == "room_occupied":
            if data.get("occupied_rooms"):
                rooms = data["occupied_rooms"]
                if len(rooms) == 1:
                    room = rooms[0]
                    return f"Room {room['room_number']} is currently occupied by {room['guest_name']}. Check-in: {room['check_in']}, Check-out: {room['check_out']}."
                else:
                    return f"There are {len(rooms)} occupied rooms. Here are the details: {', '.join([r['room_number'] for r in rooms[:5]])}"
            else:
                summary = data.get("room_summary", {})
                return f"Currently, {summary.get('occupied', 0)} rooms are occupied out of {summary.get('total_rooms', 0)} total rooms."
        
        elif intent == "room_available":
            summary = data.get("room_summary", {})
            return f"There are {summary.get('available', 0)} available rooms out of {summary.get('total_rooms', 0)} total rooms. {summary.get('cleaning', 0)} rooms are being cleaned."
        
        elif intent == "room_status":
            summary = data.get("room_summary", {})
            total = summary.get('total_rooms', 0)
            available = summary.get('available', 0)
            occupied = summary.get('occupied', 0)
            maintenance = summary.get('maintenance', 0)
            cleaning = summary.get('cleaning', 0)
            occupancy_rate = summary.get('occupancy_rate', 0)
            
            response = f"We have {total} total rooms. "
            response += f"Currently: {available} available, {occupied} occupied, {cleaning} being cleaned, and {maintenance} under maintenance. "
            response += f"Occupancy rate: {occupancy_rate}%."
            return response
        
        elif intent == "room_problems":
            problems = data.get("problems", [])
            if problems:
                response = f"⚠️ Room Issues: There are {len(problems)} rooms with problems:\n\n"
                for i, problem in enumerate(problems[:10], 1):
                    response += f"{i}. Room {problem['room_number']}\n"
                    response += f"   Issue: {problem.get('issue_type', 'Unknown')}\n"
                    if problem.get('description'):
                        response += f"   Details: {problem['description']}\n"
                    if problem.get('status'):
                        response += f"   Status: {problem['status']}\n"
                    response += "\n"
                if len(problems) > 10:
                    response += f"... and {len(problems) - 10} more rooms with issues."
                return response
            else:
                return "✅ No room problems reported at this time. All rooms are in good condition!"
        
        elif intent == "booking_by_number":
            booking = data.get("booking")
            if booking:
                response = f"Booking {booking['booking_number']}:\n"
                response += f"• Status: {booking['status']}\n"
                response += f"• Guest: {booking['guest_name']}\n"
                response += f"• Room: {booking['room_number']}\n"
                response += f"• Check-in: {booking['check_in']}\n"
                response += f"• Check-out: {booking['check_out']}\n"
                response += f"• Payment Status: {booking['payment_status']}\n"
                if booking.get('total_price'):
                    response += f"• Total Price: ${float(booking['total_price']):,.2f}"
                return response
            else:
                return "I couldn't find that booking. Please check the booking number and try again."
        
        elif intent == "booking_status":
            # Role-aware booking status
            if is_customer:
                user_bookings = data.get("user_bookings", [])
                if user_bookings:
                    response = f"You have {len(user_bookings)} booking(s):\n\n"
                    for booking in user_bookings[:5]:
                        response += f"• {booking.get('booking_number', 'N/A')}: {booking.get('status', 'Unknown')}\n"
                        response += f"  Check-in: {booking.get('check_in', 'N/A')}, Check-out: {booking.get('check_out', 'N/A')}\n"
                    if len(user_bookings) > 5:
                        response += f"\n... and {len(user_bookings) - 5} more bookings."
                    return response
                else:
                    return "You don't have any bookings at this time."
            else:
                summary = data.get("booking_summary", {})
                if summary:
                    response = "Booking Status Summary:\n"
                    response += f"• Active Bookings: {summary.get('active_bookings', 0)}\n"
                    response += f"• Upcoming Check-ins: {summary.get('upcoming_checkins', 0)}\n"
                    response += f"• Upcoming Check-outs: {summary.get('upcoming_checkouts', 0)}\n"
                    response += f"• Pending Bookings: {summary.get('pending_bookings', 0)}"
                    return response
                return "No booking data available at this time."
        
        elif intent == "invoice_by_number":
            invoice = data.get("invoice")
            if invoice:
                response = f"Invoice {invoice['invoice_number']}:\n"
                response += f"• Status: {invoice['status']}\n"
                response += f"• Amount: ${invoice['amount']:,.2f}\n"
                if invoice.get('due_date'):
                    response += f"• Due Date: {invoice['due_date']}\n"
                if invoice.get('booking_number'):
                    response += f"• Booking: {invoice['booking_number']}"
                return response
            else:
                return "I couldn't find that invoice. Please check the invoice number."
        
        elif intent == "invoice_status":
            # Role-aware invoice status
            if is_customer:
                user_invoices = data.get("user_invoices", [])
                if user_invoices:
                    response = f"You have {len(user_invoices)} invoice(s):\n\n"
                    for invoice in user_invoices[:5]:
                        status_emoji = "✅" if invoice.get('status') == 'paid' else "⏳" if invoice.get('status') == 'sent' else "⚠️"
                        response += f"{status_emoji} Invoice {invoice.get('invoice_number', 'N/A')}: {invoice.get('status', 'Unknown')}\n"
                        response += f"   Amount: ${invoice.get('total_amount', 0):,.2f}, Due: {invoice.get('due_date', 'N/A')}\n"
                    if len(user_invoices) > 5:
                        response += f"\n... and {len(user_invoices) - 5} more invoices."
                    return response
                else:
                    return "You don't have any invoices at this time."
            else:
                summary = data.get("invoice_summary", {})
                if summary:
                    response = "Invoice Status Summary:\n"
                    response += f"• Total Invoices: {summary.get('total_invoices', 0)}\n"
                    response += f"• Overdue Invoices: {summary.get('overdue_invoices', 0)}"
                    if summary.get('status_breakdown'):
                        response += f"\n• Status Breakdown: {summary['status_breakdown']}"
                    return response
                return "No invoice data available at this time."
        
        elif intent == "all_payments":
            # Display all payments in table format
            all_payments = data.get("all_payments", [])
            if all_payments:
                response = f"📋 ALL PAYMENTS ({len(all_payments)} total):\n\n"
                response += "┌──────────────┬──────────────────────────────┬──────────────────┬──────────┬──────────────────┬──────────┬────────────┬──────────────┐\n"
                response += "│ Transaction  │ Booking Number               │ Customer         │ Method   │ Type             │ Status   │ Amount     │ Payment Date │\n"
                response += "├──────────────┼──────────────────────────────┼──────────────────┼──────────┼──────────────────┼──────────┼────────────┼──────────────┤\n"
                
                for payment in all_payments:
                    trans_id = (payment.get('transaction_id') or f"PAY-{payment.get('id', 'N/A')}")[:12]
                    booking = (payment.get('booking_number') or 'N/A')[:28]
                    customer = (payment.get('customer_name') or 'N/A')[:16]
                    method = str(payment.get('payment_method', 'N/A')).replace('_', ' ').title()[:8]
                    ptype = payment.get('payment_type_display', payment.get('payment_type', 'N/A'))[:16]
                    status = str(payment.get('payment_status', 'N/A')).title()
                    
                    status_emoji = "✅" if status.lower() == 'completed' else "⏳" if status.lower() == 'pending' else "❌" if status.lower() == 'failed' else "🔄" if status.lower() == 'refunded' else "❓"
                    status_display = f"{status_emoji} {status}"[:10]
                    
                    amount = payment.get('amount', 0)
                    amount_str = f"{amount:,.2f} €"[:12]
                    
                    pdate = payment.get('payment_date', None)
                    if pdate and pdate != 'Invalid Date':
                        try:
                            from datetime import datetime
                            if isinstance(pdate, str):
                                if 'T' in pdate:
                                    dt = datetime.fromisoformat(pdate.replace('Z', '+00:00'))
                                    pdate = dt.strftime('%Y-%m-%d')
                                else:
                                    pdate = pdate[:10]
                            else:
                                pdate = pdate.strftime('%Y-%m-%d') if hasattr(pdate, 'strftime') else str(pdate)[:10]
                        except Exception:
                            pdate = "Invalid Date"
                    else:
                        pdate = "Invalid Date"
                    
                    response += f"│ {trans_id:<12} │ {booking:<28} │ {customer:<16} │ {method:<8} │ {ptype:<16} │ {status_display:<10} │ {amount_str:<12} │ {pdate:<14} │\n"
                
                response += "└──────────────┴──────────────────────────────┴──────────────────┴──────────┴──────────────────┴──────────┴────────────┴──────────────┘\n"
                
                # Add summary statistics
                completed = sum(1 for p in all_payments if p.get('payment_status', '').lower() == 'completed')
                pending = sum(1 for p in all_payments if p.get('payment_status', '').lower() == 'pending')
                failed = sum(1 for p in all_payments if p.get('payment_status', '').lower() == 'failed')
                refunded = sum(1 for p in all_payments if p.get('payment_status', '').lower() == 'refunded')
                total_amount = sum(float(p.get('amount', 0)) for p in all_payments)
                completed_amount = sum(float(p.get('amount', 0)) for p in all_payments if p.get('payment_status', '').lower() == 'completed')
                pending_amount = sum(float(p.get('amount', 0)) for p in all_payments if p.get('payment_status', '').lower() == 'pending')
                
                response += f"\n📊 Summary:\n"
                response += f"• Total Payments: {len(all_payments)}\n"
                response += f"• ✅ Completed: {completed} ({completed_amount:,.2f} €)\n"
                response += f"• ⏳ Pending: {pending} ({pending_amount:,.2f} €)\n"
                response += f"• ❌ Failed: {failed}\n"
                if refunded > 0:
                    response += f"• 🔄 Refunded: {refunded}\n"
                response += f"• Total Amount: {total_amount:,.2f} €"
                
                return response
            else:
                return "No payments found."
        
        elif intent == "payment_status":
            # Role-aware payment status
            if is_customer:
                user_payments = data.get("user_payments", [])
                if user_payments:
                    response = f"Your recent payments ({len(user_payments)} total):\n\n"
                    for payment in user_payments[:5]:
                        status_emoji = "✅" if payment.get('payment_status') == 'completed' else "⏳" if payment.get('payment_status') == 'pending' else "❌"
                        response += f"{status_emoji} ${payment.get('amount', 0):,.2f} - {payment.get('payment_method', 'N/A')}\n"
                        response += f"   Status: {payment.get('payment_status', 'Unknown')}, Date: {payment.get('payment_date', 'N/A')}\n"
                    if len(user_payments) > 5:
                        response += f"\n... and {len(user_payments) - 5} more payments."
                    return response
                else:
                    return "You don't have any payment records at this time."
            else:
                summary = data.get("payment_summary", {})
                if summary:
                    response = "Payment Status Summary:\n"
                    response += f"• Total Revenue (last 30 days): ${summary.get('total_revenue', 0):,.2f}\n"
                    response += f"• Pending Payments: ${summary.get('pending_amount', 0):,.2f}\n"
                    response += f"• Completed Payments: {summary.get('completed_payments', 0)}"
                    return response
                return "No payment data available at this time."
        
        elif intent == "chat_status":
            chats = data.get("unanswered_chats", [])
            if chats:
                response = f"Chat Status: There are {len(chats)} unanswered chat messages.\n\n"
                response += "Oldest waiting messages:\n"
                for i, chat in enumerate(chats[:3], 1):
                    response += f"{i}. {chat['visitor_name']} - Waiting {chat['waiting_hours']:.1f} hours\n"
                    response += f"   Last message: {chat['last_message'][:50]}...\n"
                return response
            else:
                return "All chat messages have been answered. Great job! ✅"
        
        elif intent == "general_status":
            # Comprehensive status overview - always show all sections for consistency
            room_summary = data.get("room_summary", {})
            booking_summary = data.get("booking_summary", {})
            payment_summary = data.get("payment_summary", {})
            invoice_summary = data.get("invoice_summary", {})
            unanswered_chats = data.get("unanswered_chats", [])
            problems = data.get("room_problems", [])
            
            response = "📊 Hotel Status Overview:\n\n"
            
            # Rooms - always show
            response += "🏨 ROOMS:\n"
            occupancy_rate = room_summary.get('occupancy_rate', 0)
            # Format occupancy rate to show one decimal place
            if isinstance(occupancy_rate, (int, float)):
                occupancy_rate = f"{occupancy_rate:.1f}"
            response += f"• Total: {room_summary.get('total_rooms', 0)}\n"
            response += f"• Available: {room_summary.get('available', 0)}\n"
            response += f"• Occupied: {room_summary.get('occupied', 0)}\n"
            response += f"• Cleaning: {room_summary.get('cleaning', 0)}\n"
            response += f"• Maintenance: {room_summary.get('maintenance', 0)}\n"
            response += f"• Occupancy Rate: {occupancy_rate}%\n\n"
            
            # Bookings - always show
            response += "📅 BOOKINGS:\n"
            response += f"• Active: {booking_summary.get('active_bookings', 0)}\n"
            response += f"• Upcoming Check-ins: {booking_summary.get('upcoming_checkins', 0)}\n"
            response += f"• Upcoming Check-outs: {booking_summary.get('upcoming_checkouts', 0)}\n"
            response += f"• Pending: {booking_summary.get('pending_bookings', 0)}\n\n"
            
            # Payments - always show
            response += "💰 PAYMENTS:\n"
            total_revenue = payment_summary.get('total_revenue', 0) or 0
            pending_amount = payment_summary.get('pending_amount', 0) or 0
            response += f"• Revenue (30 days): ${total_revenue:,.2f}\n"
            response += f"• Pending: ${pending_amount:,.2f}\n"
            response += f"• Completed: {payment_summary.get('completed_payments', 0)}\n\n"
            
            # Invoices - always show
            response += "🧾 INVOICES:\n"
            response += f"• Total: {invoice_summary.get('total_invoices', 0)}\n"
            response += f"• Overdue: {invoice_summary.get('overdue_invoices', 0)}\n\n"
            
            # Chats - always show
            response += "💬 CHATS:\n"
            response += f"• Unanswered: {len(unanswered_chats)}\n\n"
            
            # Problems - show if any, otherwise skip
            if problems:
                response += "⚠️ ISSUES:\n"
                response += f"• Rooms with problems: {len(problems)}\n"
                for problem in problems[:3]:
                    response += f"  - Room {problem['room_number']}: {problem.get('description', 'Issue reported')}\n"
            
            return response
        
        elif intent == "app_info":
            # Comprehensive application information - formatted for admin
            app_knowledge = data.get("app_knowledge", {})
            role_name = self.role_access_service.role_name if self.role_access_service else "customer"
            
            if role_name != "admin":
                response = "📱 Application Overview:\n\n"
                response += app_knowledge.get("overview", "This is a Hotel Booking and Management System.")
                response += "\n\n"
                role_info = app_knowledge.get("role_info", {})
                if role_info:
                    response += f"Your Role: {role_name.upper()}\n"
                    response += f"Description: {role_info.get('description', 'Standard user access')}\n"
                return response
            
            # Admin gets full comprehensive information
            response = "📱 Hotel Booking & Management System (HBMS) - Complete Overview\n\n"
            response += "=" * 60 + "\n\n"
            
            # Overview
            response += "📋 SYSTEM OVERVIEW:\n"
            response += app_knowledge.get("overview", "") + "\n\n"
            
            # Core Features
            features = app_knowledge.get("features", {})
            if features:
                response += "🔧 CORE FEATURES & MODULES:\n\n"
                feature_count = 0
                for feature_name, feature_info in features.items():
                    feature_count += 1
                    feature_title = feature_name.replace("_", " ").title()
                    response += f"{feature_count}. {feature_title}\n"
                    response += f"   {feature_info.get('description', 'N/A')}\n"
                    
                    if feature_info.get('models'):
                        models = feature_info.get('models', [])
                        if models:
                            response += f"   Models: {', '.join(models[:5])}"
                            if len(models) > 5:
                                response += f" (+{len(models) - 5} more)"
                            response += "\n"
                    
                    if feature_info.get('statuses'):
                        statuses = feature_info.get('statuses', [])
                        response += f"   Statuses: {', '.join(statuses)}\n"
                    
                    response += "\n"
            
            # User Roles
            role_capabilities = app_knowledge.get("role_capabilities", {})
            if role_capabilities:
                response += "👥 USER ROLES & PERMISSIONS:\n\n"
                for role_name_key, role_info in role_capabilities.items():
                    response += f"• {role_name_key.upper()}\n"
                    response += f"  {role_info.get('description', '')}\n"
                    
                    can_access = role_info.get('can_access', [])
                    if can_access:
                        response += f"  Can Access: {len(can_access)} features\n"
                        # Show first 5 capabilities
                        for cap in can_access[:5]:
                            response += f"    - {cap}\n"
                        if len(can_access) > 5:
                            response += f"    ... and {len(can_access) - 5} more\n"
                    
                    cannot_access = role_info.get('cannot_access', [])
                    if cannot_access:
                        response += f"  Restrictions: {len(cannot_access)} items\n"
                    
                    response += "\n"
            
            # Technology Stack
            response += "💻 TECHNOLOGY STACK:\n\n"
            response += "• Backend: FastAPI (Python)\n"
            response += "• Frontend: React/TypeScript\n"
            response += "• Database: SQLAlchemy ORM\n"
            response += "• AI: Self-learning assistant with pattern recognition\n"
            response += "• Authentication: JWT with MFA support\n"
            response += "• Security: Role-based access control, audit logging\n\n"
            
            # Key Capabilities
            response += "✨ KEY CAPABILITIES:\n\n"
            response += "• Complete booking lifecycle management\n"
            response += "• Multi-role access control (Admin, Staff, Accountant, Customer)\n"
            response += "• Payment processing with multiple methods\n"
            response += "• Invoice generation and tracking\n"
            response += "• Loyalty program with points and rewards\n"
            response += "• Real-time chat support system\n"
            response += "• Room maintenance and housekeeping management\n"
            response += "• Guest profile and CRM features\n"
            response += "• Analytics and reporting\n"
            response += "• Content management (blog, pages, banners)\n"
            response += "• Email campaigns and marketing automation\n"
            response += "• Group booking management\n"
            response += "• Dynamic pricing and rate plans\n"
            response += "• Package deals (room + services)\n"
            response += "• Review and rating system\n"
            response += "• Audit logging and security compliance\n\n"
            
            # Common Queries
            common_queries = app_knowledge.get("common_queries", {})
            if common_queries:
                response += "💬 COMMON QUERY CATEGORIES:\n\n"
                for category, queries in common_queries.items():
                    response += f"• {category.title()}: {', '.join(queries[:3])}"
                    if len(queries) > 3:
                        response += f" (+{len(queries) - 3} more)"
                    response += "\n"
                response += "\n"
            
            response += "=" * 60 + "\n"
            response += "\n💡 TIP: Ask me about any specific feature, model, or capability for detailed information!"
            
            return response
        
        elif intent == "general_query":
            # Try to provide helpful response based on available data
            room_summary = data.get("room_summary", {})
            booking_summary = data.get("booking_summary", {})
            payment_summary = data.get("payment_summary", {})
            
            response = "I can help you with information about:\n\n"
            response += "🏨 Rooms - availability, occupancy, status, problems\n"
            response += "📅 Bookings - active, upcoming, pending reservations\n"
            response += "💰 Payments - revenue, pending payments, transactions\n"
            response += "🧾 Invoices - status, overdue invoices\n"
            response += "💬 Chats - unanswered customer messages\n\n"
            
            if room_summary:
                response += f"Quick Stats: {room_summary.get('total_rooms', 0)} total rooms, "
                response += f"{room_summary.get('occupied', 0)} occupied, "
                response += f"{room_summary.get('available', 0)} available.\n\n"
            
            response += "Try asking:\n"
            response += "• 'How many rooms do we have?'\n"
            response += "• 'Show me occupied rooms'\n"
            response += "• 'What's our revenue?'\n"
            response += "• 'Are there any unanswered chats?'"
            
            return response
        
        else:
            return "I can help you with questions about rooms, bookings, invoices, payments, and chat messages. What would you like to know?"
    
    def _apply_learned_pattern(self, pattern: AILearnedPattern, data: Dict, query: str) -> Optional[str]:
        """Apply learned pattern to generate response"""
        try:
            response_template = pattern.response_template or ""
            
            # Replace placeholders with actual data
            if "{data}" in response_template and data:
                # Inject relevant data into response
                data_str = json.dumps(data, indent=2, default=str)[:500]
                response = response_template.replace("{data}", data_str)
            else:
                response = response_template
            
            return response[:2000]  # Limit response length
        except Exception as e:
            logger.error(f"Error applying learned pattern: {str(e)}", exc_info=True)
            return None

