
"""
Mock Ollama service for testing and development
Simulates real Ollama API responses for debugging streaming functionality
"""
import asyncio
import json
from typing import Dict, Any, List, Optional
from .models import Agent
import random
import time

# Mock response templates based on agent roles
MOCK_RESPONSES = {
    "generator": [
        "• Implementarea unei platforme educaționale bazate pe realitate augmentată pentru vizualizarea conceptelor complexe",
        "• Dezvoltarea unui sistem de gamificare integrată care transformă lecțiile în experiențe interactive",
        "• Crearea unor asistenti AI personalizați pentru fiecare student, care să se adapteze stilului de învățare individual",
        "• Utilizarea blockchain-ului pentru certificarea și verificarea competențelor dobândite",
        "• Implementarea laboratorelor virtuale pentru experimentare în domenii precum chimia și fizica"
    ],
    
    "critic": [
        "• RISC: Costurile mari de implementare AR pot fi prohibitive pentru multe instituții",
        "• LIMITARE: Dependența de tehnologie poate reduce interacțiunea umană directă",
        "• ÎMBUNĂTĂȚIRE: Propun un model hibrid care combină tehnologia cu metode tradiționale",
        "• RISC: Protecția datelor personale ale studenților în sistemele AI",
        "• ALTERNATIVĂ: Începe cu programe pilot în școli selectate pentru testare"
    ],
    
    "moderator": [
        "• PROGRES: Am identificat 3 soluții inovatoare și 2 îmbunătățiri practice",
        "• ÎNTREBARE FOCALIZATĂ: Care sunt prioritățile de implementare pe termen scurt?",
        "• NOUTATE NECESARĂ: Lipsește abordarea pentru formarea profesorilor",
        "• URMĂTORUL PAS: Să explorăm modelele de finanțare pentru aceste inițiative",
        "• CLARIFICARE: Cum măsurăm impactul asupra rezultatelor educaționale?"
    ],
    
    "sintetizator": [
        "• SINTEZĂ: Combinând AR cu gamificarea, obținem o experiență educațională immersivă",
        "• FOAIE DE PARCURS: 1) Pilot în 5 școli, 2) Formare profesori, 3) Evaluare rezultate",
        "• OPTIMIZARE: Eliminăm redundanța între sistemele AI și concentrăm pe o singură platformă",
        "• IMPLEMENTARE: Start cu discipline STEM, apoi extindere la alte materii",
        "• BUGET ESTIMAT: 500K euro pentru faza pilot de 6 luni"
    ]
}

class MockOllamaService:
    """Mock service that simulates Ollama API responses"""
    
    @staticmethod
    def should_use_mock(agent_url: str) -> bool:
        """Determine if we should use mock based on agent URL or environment"""
        # Use mock for localhost or when Ollama is not available
        return agent_url.startswith("127.0.0.1") or agent_url.startswith("localhost")
    
    @staticmethod
    async def mock_stream_response(
        agent: Agent,
        messages: List[Dict[str, str]], 
        callback: Optional[callable] = None
    ) -> Dict[str, Any]:
        """
        Mock streaming response that simulates real Ollama behavior
        """
        try:
            # Get appropriate mock response for role
            role = agent.rol.lower()
            responses = MOCK_RESPONSES.get(role, MOCK_RESPONSES["generator"])
            
            # Select a random response
            full_response = random.choice(responses)
            
            # Add some context based on messages
            if len(messages) > 2:
                context_intro = f"\nBazându-mă pe discuția anterioară, "
                full_response = context_intro + full_response.lower()
            
            # Simulate token-by-token streaming
            tokens = full_response.split()
            token_count = 0
            
            for i, token in enumerate(tokens):
                if i > 0:
                    token = " " + token  # Add space before tokens except first
                
                # Simulate network delay
                await asyncio.sleep(random.uniform(0.05, 0.15))
                
                # Call callback for each token (simulating real streaming)
                if callback:
                    await callback(token)
                
                token_count += 1
                
                # Simulate some processing time for longer responses
                if token_count % 10 == 0:
                    await asyncio.sleep(0.1)
            
            # Add final punctuation
            final_token = "."
            if callback:
                await callback(final_token)
            
            return {
                "text": full_response + final_token,
                "tokens": len(tokens) + 1,
                "success": True
            }
            
        except Exception as e:
            return {
                "text": None,
                "tokens": None,
                "success": False,
                "error": f"Mock error: {str(e)}"
            }
    
    @staticmethod
    async def get_mock_models() -> List[str]:
        """Return mock model list"""
        return [
            "llama2:latest",
            "llama2:13b",
            "codellama:latest",
            "mistral:latest",
            "neural-chat:latest"
        ]
    
    @staticmethod
    async def test_mock_connection() -> Dict[str, Any]:
        """Return mock connection test result"""
        return {
            "success": True,
            "message": "Mock connection successful. 5 models disponibile.",
            "models": await MockOllamaService.get_mock_models()
        }


async def enhanced_stream_chat_response(
    agent: Agent,
    messages: List[Dict[str, str]],
    callback: Optional[callable] = None
) -> Dict[str, Any]:
    """
    Enhanced streaming function that uses mock when Ollama is not available
    """
    from .ollama_service import stream_chat_response
    
    # Try real Ollama first
    try:
        result = await stream_chat_response(agent, messages, callback)
        if result["success"]:
            return result
    except Exception as e:
        print(f"Ollama connection failed: {e}")
    
    # Fall back to mock if real Ollama fails
    print(f"Using mock response for agent {agent.url} with role {agent.rol}")
    return await MockOllamaService.mock_stream_response(agent, messages, callback)


async def enhanced_get_available_models(ip: str, port: str = "11434", timeout: int = 10) -> List[str]:
    """
    Enhanced model listing with mock fallback
    """
    from .ollama_service import get_available_models
    
    # Try real Ollama first
    try:
        return await get_available_models(ip, port, timeout)
    except Exception as e:
        print(f"Ollama models request failed: {e}")
        # Return mock models for testing
        return await MockOllamaService.get_mock_models()


async def enhanced_test_ollama_connection(ip: str, port: str = "11434") -> Dict[str, Any]:
    """
    Enhanced connection test with mock fallback
    """
    from .ollama_service import test_ollama_connection
    
    # Try real Ollama first
    try:
        result = await test_ollama_connection(ip, port)
        if result["success"]:
            return result
    except Exception as e:
        print(f"Ollama connection test failed: {e}")
    
    # Return mock success for testing
    return await MockOllamaService.test_mock_connection()
