import os
from openai import OpenAI

class Native1API:
    """
    Unified client wrapper for the Native1API edge reseller gateway.
    Routes requests through the edge proxy while protecting credentials.
    """
    def __init__(self, firebase_jwt_token: str = None, base_url: str = "https://api.native1api.com/v1"):
        self.api_key = firebase_jwt_token or os.environ.get("NATIVE1_SESSION_TOKEN")
        if not self.api_key:
            raise ValueError("An active Firebase ID Token must be provided for edge authentication.")
        
        self.client = OpenAI(
            api_key=self.api_key,
            base_url=base_url
        )

    def generate_chat_completion(self, model: str, messages: list, temperature: float = 0.7) -> str:
        try:
            response = self.client.chat.completions.create(
                model=model,
                messages=messages,
                temperature=temperature
            )
            return response.choices[0].message.content
        except Exception as err:
            raise RuntimeError(f"Native1API Gateway Routing Failure: {str(err)}")

# Usage Example
if __name__ == "__main__":
    # Initialize client using active Firebase token
    client = Native1API(firebase_jwt_token="eyJhbGciOiJSUzI1NiIsImtpZCI6IjEifQ...")
    
    chat_prompt = [
        {"role": "user", "content": "Explain the advantages of V8 edge workers vs standard VM containers."}
    ]
    
    completion = client.generate_chat_completion(
        model="@cf/meta/llama-3.2-3b-instruct",
        messages=chat_prompt
    )
    print("Response:\n", completion)
