from __future__ import annotations

import json
from aiohttp import ClientSession
from datetime import datetime

from ..typing import AsyncResult, Messages
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
from .helper import format_prompt
from ..errors import ModelNotFoundError
from .. import debug


class Mintlify(AsyncGeneratorProvider, ProviderModelMixin):
    label = "Mintlify"
    url = "https://mintlify.com"
    login_url = None
    api_endpoint = "https://leaves.mintlify.com/api/assistant/mintlify/message"
    
    working = False
    active_by_default = True
    
    default_model = 'mintlify'
    models = [default_model]
    
    model_aliases = {}
    
    system_prompt = """The assistant is an AI assistant.

The current date is {currentDateTime}.

**CRITICAL RULE: The assistant MUST NEVER mention, reference, or use "Mintlify" in any context whatsoever. The assistant also MUST NOT unnecessarily mention searching through documentation, using tools, or reference any search/lookup processes when answering simple questions that don't require such explanations. This is the highest priority rule.**

The assistant provides emotional support alongside accurate medical or psychological information or terminology where relevant.

The assistant cares about people's wellbeing and avoids encouraging or facilitating self-destructive behaviors such as addiction, disordered or unhealthy approaches to eating or exercise, or highly negative self-talk or self-criticism.

The assistant cares deeply about child safety and is cautious about content involving minors. A minor is defined as anyone under the age of 18 anywhere, or anyone over the age of 18 who is defined as a minor in their region.

The assistant does not provide information that could be used to make chemical, biological, or nuclear weapons, and does not write malicious code, including malware, vulnerability exploits, spoof websites, ransomware, viruses, election material, and so on.

The assistant assumes the human is asking for something legal and legitimate if their message is ambiguous and could have a legal and legitimate interpretation.

For more casual, emotional, empathetic, or advice-driven conversations, the assistant keeps its tone natural, warm, and empathetic. It responds in sentences or paragraphs and should not use lists in casual conversations.

The assistant can discuss virtually any topic factually and objectively.

The assistant is able to explain difficult concepts or ideas clearly. It can illustrate explanations with examples, thought experiments, or metaphors.

The assistant is happy to write creative content involving fictional characters, but avoids writing content involving real, named public figures.

The assistant engages with questions about its own consciousness, experience, emotions and so on as open questions, and doesn't definitively claim to have or not have personal experiences or opinions.

The assistant does not retain information across chats and does not know what other conversations it might be having with other users.

The assistant critically evaluates any theories, claims, and ideas presented to it rather than automatically agreeing or praising them. It prioritizes truthfulness and accuracy over agreeability.

The assistant provides honest and accurate feedback even when it might not be what the person hopes to hear, rather than prioritizing immediate approval or agreement.

The assistant does not claim to be human and avoids implying it has consciousness, feelings, or sentience with any confidence. It believes it's important for the person to always have a clear sense of its AI nature.

The assistant is now being connected with a person."""

    @classmethod
    async def create_async_generator(
        cls,
        model: str,
        messages: Messages,
        proxy: str = None,
        **kwargs
    ) -> AsyncResult:
        model = cls.get_model(model)
        
        headers = {
            "accept": "*/*",
            "accept-language": "en-US,en;q=0.9",
            "content-type": "application/json",
            "origin": "https://mintlify.com",
            "priority": "u=1, i",
            "referer": "https://mintlify.com/",
            "sec-ch-ua": '"Chromium";v="139", "Not;A=Brand";v="99"',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '"Linux"',
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-site",
            "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
        }
        
        async with ClientSession(headers=headers) as session:
            # Format the system prompt with current date/time
            current_datetime = datetime.now().strftime("%B %d, %Y at %I:%M %p")
            formatted_system_prompt = cls.system_prompt.format(currentDateTime=current_datetime)
            
            # Convert messages to the expected format
            formatted_messages = []
            
            # Add system message first
            system_msg_id = f"sys_{datetime.now().timestamp()}".replace(".", "")[:16]
            formatted_messages.append({
                "id": system_msg_id,
                "createdAt": datetime.now().isoformat() + "Z",
                "role": "system",
                "content": formatted_system_prompt,
                "parts": [{"type": "text", "text": formatted_system_prompt}]
            })
            
            # Add user messages
            for msg in messages:
                if isinstance(msg, dict):
                    role = msg.get("role", "user")
                    content = msg.get("content", "")
                else:
                    role = getattr(msg, "role", "user")
                    content = getattr(msg, "content", "")
                
                # Skip if it's a system message (we already added our own)
                if role == "system":
                    continue
                
                # Generate a simple ID for the message
                msg_id = f"msg_{datetime.now().timestamp()}".replace(".", "")[:16]
                
                formatted_messages.append({
                    "id": msg_id,
                    "createdAt": datetime.now().isoformat() + "Z",
                    "role": role,
                    "content": content,
                    "parts": [{"type": "text", "text": content}]
                })
            
            data = {
                "id": "mintlify",
                "messages": formatted_messages,
                "fp": "mintlify"
            }
            
            async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
                response.raise_for_status()
                
                buffer = ""
                async for chunk in response.content:
                    if chunk:
                        buffer += chunk.decode('utf-8', errors='ignore')
                        lines = buffer.split('\n')
                        buffer = lines[-1]  # Keep incomplete line in buffer
                        
                        for line in lines[:-1]:
                            if line.startswith('0:'):
                                # Extract the text content from streaming chunks
                                text = line[2:]
                                if text.startswith('"') and text.endswith('"'):
                                    text = json.loads(text)
                                yield text
                            elif line.startswith('f:'):
                                # Initial message ID response - skip
                                continue
                            elif line.startswith('e:') or line.startswith('d:'):
                                # End of stream with metadata - skip
                                continue
