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 = ""
    
    # 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_api_base_url: str = "https://api.anthropic.com"
    
    # 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
    
    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", "")
        
        # Load from tokens.txt if not found in env
        if not self.claude_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()
                            break
            except FileNotFoundError:
                pass
        
        # For MVP, we'll use the same API key for both pro and regular
        # In practice, Claude Pro might use a different endpoint or key
        self.claude_pro_api_key = self.claude_api_key

# Global config instance
config = Config()