# src/utils/config.py
"""
Configuration management for the self-improving code generator
"""

import os
from typing import Optional
from pydantic_settings import BaseSettings  # Updated import
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

class Settings(BaseSettings):
    """Application settings"""
    
    # Database configuration
    database_url: str = os.getenv("DATABASE_URL", "postgresql://pipeline_admin:secure_pipeline_2024@postgres:5432/dev_pipeline")
    
    # Claude API configuration
    claude_api_key: str = os.getenv("CLAUDE_API_KEY", "")
    claude_model: str = os.getenv("CLAUDE_MODEL", "claude-3-5-sonnet-20241022")
    
    # Redis configuration
    redis_url: str = os.getenv("REDIS_URL", "redis://pipeline_redis:6379")
    
    # Service configuration
    service_port: int = int(os.getenv("SERVICE_PORT", "8007"))
    service_host: str = os.getenv("SERVICE_HOST", "0.0.0.0")
    
    # Logging configuration
    log_level: str = os.getenv("LOG_LEVEL", "INFO")
    
    # Improvement settings
    default_target_quality: float = float(os.getenv("DEFAULT_TARGET_QUALITY", "0.85"))
    max_iterations: int = int(os.getenv("MAX_ITERATIONS", "5"))
    
    # Feature flags
    enable_learning_system: bool = os.getenv("ENABLE_LEARNING_SYSTEM", "true").lower() == "true"
    enable_background_processing: bool = os.getenv("ENABLE_BACKGROUND_PROCESSING", "true").lower() == "true"
    
    class Config:
        env_file = ".env"
        case_sensitive = False

# Global settings instance
settings = Settings()

def get_settings() -> Settings:
    """Get application settings"""
    return settings

# Validation
def validate_configuration():
    """Validate required configuration"""
    errors = []
    
    if not settings.claude_api_key:
        errors.append("CLAUDE_API_KEY is required")
    
    if not settings.database_url:
        errors.append("DATABASE_URL is required")
    
    if errors:
        raise ValueError(f"Configuration errors: {', '.join(errors)}")

# Technology stack configurations
TECHNOLOGY_PROFILES = {
    "nodejs_react": {
        "entry_points": ["App.js", "App.tsx", "index.js", "index.tsx"],
        "config_files": ["package.json", ".gitignore", "README.md"],
        "folder_structure": ["src/components", "src/pages", "public"],
        "linting_tools": ["eslint", "prettier"],
        "testing_tools": ["jest", "react-testing-library"]
    },
    "python_fastapi": {
        "entry_points": ["main.py", "app.py"],
        "config_files": ["requirements.txt", ".gitignore", "README.md"],
        "folder_structure": ["app/models", "app/routers", "tests"],
        "linting_tools": ["pylint", "black", "mypy"],
        "testing_tools": ["pytest", "pytest-asyncio"]
    },
    "java_spring": {
        "entry_points": ["Application.java", "*Application.java"],
        "config_files": ["pom.xml", ".gitignore", "README.md"],
        "folder_structure": ["src/main/java", "src/test/java"],
        "linting_tools": ["checkstyle", "spotbugs"],
        "testing_tools": ["junit", "mockito"]
    }
}

# Quality thresholds
QUALITY_THRESHOLDS = {
    "critical": 30,    # Below this is critical
    "poor": 50,        # Below this is poor quality
    "good": 70,        # Above this is good quality
    "excellent": 85    # Above this is excellent quality
}
