# services/recipes/app/core/config.py
"""
Configuration management for Recipe Service
"""

import os
from typing import Optional


class Settings:
    """Recipe service configuration settings"""
    
    # Service identification
    SERVICE_NAME: str = "recipes"
    SERVICE_VERSION: str = "1.0.0"
    
    # API settings
    API_V1_PREFIX: str = "/api/v1"
    
    # Database configuration (secure approach - build from components)
    @property
    def DATABASE_URL(self) -> str:
        """Build database URL from secure components"""
        # Try complete URL first (for backward compatibility)
        complete_url = os.getenv("RECIPES_DATABASE_URL")
        if complete_url:
            return complete_url

        # Build from components (secure approach)
        user = os.getenv("RECIPES_DB_USER", "recipes_user")
        password = os.getenv("RECIPES_DB_PASSWORD", "recipes_pass123")
        host = os.getenv("RECIPES_DB_HOST", "localhost")
        port = os.getenv("RECIPES_DB_PORT", "5432")
        name = os.getenv("RECIPES_DB_NAME", "recipes_db")

        return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"
    
    # Redis (if needed for caching)
    REDIS_URL: str = os.getenv("REDIS_URL", "redis://localhost:6379/0")
    
    # External service URLs
    GATEWAY_URL: str = os.getenv("GATEWAY_URL", "http://gateway-service:8000")
    INVENTORY_SERVICE_URL: str = os.getenv(
        "INVENTORY_SERVICE_URL",
        "http://inventory-service:8000"
    )
    SALES_SERVICE_URL: str = os.getenv(
        "SALES_SERVICE_URL",
        "http://sales-service:8000"
    )
    
    # Authentication
    SECRET_KEY: str = os.getenv("SECRET_KEY", "your-secret-key-here")
    JWT_SECRET_KEY: str = os.getenv("JWT_SECRET_KEY", "your-super-secret-jwt-key-change-in-production-min-32-characters-long")
    ACCESS_TOKEN_EXPIRE_MINUTES: int = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "30"))
    
    # Logging
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
    
    # Production configuration
    ENVIRONMENT: str = os.getenv("ENVIRONMENT", "development")
    DEBUG: bool = os.getenv("DEBUG", "False").lower() == "true"
    
    # CORS settings
    ALLOWED_ORIGINS: list = os.getenv("ALLOWED_ORIGINS", "http://localhost:3000").split(",")
    
    # Recipe-specific settings
    MAX_RECIPE_INGREDIENTS: int = int(os.getenv("MAX_RECIPE_INGREDIENTS", "50"))
    MAX_BATCH_SIZE_MULTIPLIER: float = float(os.getenv("MAX_BATCH_SIZE_MULTIPLIER", "10.0"))
    DEFAULT_RECIPE_VERSION: str = "1.0"
    
    # Production settings
    MAX_PRODUCTION_BATCHES_PER_DAY: int = int(os.getenv("MAX_PRODUCTION_BATCHES_PER_DAY", "100"))
    PRODUCTION_SCHEDULE_DAYS_AHEAD: int = int(os.getenv("PRODUCTION_SCHEDULE_DAYS_AHEAD", "7"))
    
    # Cost calculation settings
    OVERHEAD_PERCENTAGE: float = float(os.getenv("OVERHEAD_PERCENTAGE", "15.0"))  # Default 15% overhead
    LABOR_COST_PER_HOUR: float = float(os.getenv("LABOR_COST_PER_HOUR", "25.0"))  # Default €25/hour
    
    # Quality control
    MIN_QUALITY_SCORE: float = float(os.getenv("MIN_QUALITY_SCORE", "6.0"))  # Minimum acceptable quality score
    MAX_DEFECT_RATE: float = float(os.getenv("MAX_DEFECT_RATE", "5.0"))  # Maximum 5% defect rate
    
    # Messaging/Events (if using message queues)
    RABBITMQ_URL: Optional[str] = os.getenv("RABBITMQ_URL")
    KAFKA_BOOTSTRAP_SERVERS: Optional[str] = os.getenv("KAFKA_BOOTSTRAP_SERVERS")
    
    # Health check settings
    HEALTH_CHECK_TIMEOUT: int = int(os.getenv("HEALTH_CHECK_TIMEOUT", "30"))
    
    class Config:
        case_sensitive = True


# Global settings instance
settings = Settings()