import os
from typing import List, Dict
from openai import OpenAI
from groq import Groq

class techv_chat:
    """
    A chat interface for interacting with LLM providers via the client.
    """

    def __init__(self, client):
        """
        Initialize the chat interface.

        Args:
            client: The provider-specific API client instance.
        """
        self.client = client
    def chat(self, 
             messages: List[Dict[str, str]], 
             model: str, 
             temperature: float = 0.6, 
             max_tokens: int = 1024, 
             top_p: float = 0.9, 
             stream: bool = True):
        """
        Send a chat request to the LLM provider.

        Args:
            messages (list): List of message dictionaries in the format [{"role": "user", "content": "Hello"}].
            model (str): The model to use (must be specified by the user).
            temperature (float): Sampling temperature (default: 0.1).
            max_tokens (int): Maximum number of tokens to generate (default: 1024).
            top_p (float): Top-p sampling parameter (default: 0.9).
            stream (bool): Whether to stream the response (default: False).

        Returns:
            The response from the LLM provider.
        """
        if isinstance(self.client, OpenAI):
            response = self.client.chat.completions.create(
            model=model,
            messages=messages,
            temperature=temperature,
            max_tokens=max_tokens,
            top_p=top_p,
            stream=stream,
        )
        
        if stream:
            reasoning_content = ""
            content = ""
            
            for chunk in response:
                if hasattr(chunk.choices[0].delta, "reasoning_content"):
                    reasoning_content += chunk.choices[0].delta.reasoning_content
                else:
                    content += chunk.choices[0].delta.content
            
            return {"reasoning_content": reasoning_content, "content": content}
        
       

        elif isinstance(self.client, Groq):
            response = self.client.chat.completions.create(
                model=model,
                messages=messages,
                temperature=temperature,
                max_tokens=max_tokens,
                top_p=top_p,
                stream=stream,
            )
        else:
            raise ValueError("Unsupported client type.")

        return response
