import os
from typing import Optional
from pydantic import BaseModel

class Config(BaseModel):
    # Claude API configurations
    claude_pro_api_key: str = ""
    claude_api_key: str = ""
    deepseek_api_key: str = ""
    kimi_api_key: str = ""
    
    # Router settings
    port: int = 8000
    host: str = "0.0.0.0"
    
    # Retry settings
    max_retries: int = 3
    retry_delay: float = 1.0
    
    # API endpoints
    claude_pro_base_url: str = "https://api.anthropic.com"  # Claude Pro might use different endpoint in future
    claude_api_base_url: str = "https://api.anthropic.com"
    deepseek_base_url: str = "https://api.deepseek.com"
    kimi_base_url: str = "https://api.moonshot.ai"
    
    # Health check settings
    health_check_enabled: bool = True
    health_check_cron: str = "0-4 * * * *"  # Every hour, first 5 minutes
    health_check_message: str = "ping"
    health_check_model: str = "claude-3-haiku-20240307"  # Use cheapest model for checks
    deepseek_health_check_model: str = "deepseek-reasoner"
    kimi_health_check_model: str = "Kimi-K2-Instruct"
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # Load from environment or token file
        self.load_from_env()
    
    def load_from_env(self):
        """Load configuration from environment variables or token file"""
        # Try environment variables first
        self.claude_api_key = os.getenv("CLAUDE_API_KEY", "")
        self.deepseek_api_key = os.getenv("DEEPSEEK_API_KEY", "")
        self.kimi_api_key = os.getenv("KIMI_API_KEY", "")
        
        # Load from tokens.txt if not found in env
        if not self.claude_api_key or not self.deepseek_api_key or not self.kimi_api_key:
            try:
                with open("/home/will/docker/tokens.txt", "r") as f:
                    for line in f:
                        if line.startswith("claude_API="):
                            self.claude_api_key = line.split("=", 1)[1].strip()
                        elif line.startswith("deepseek_API="):
                            self.deepseek_api_key = line.split("=", 1)[1].strip()
                        elif line.startswith("kimi_API="):
                            self.kimi_api_key = line.split("=", 1)[1].strip()
            except FileNotFoundError:
                pass
        
        # Claude Pro uses the same API key as regular Claude API
        # but may use different endpoint or have different rate limits
        self.claude_pro_api_key = self.claude_api_key

# Global config instance
config = Config()