# shared/config/base.py
"""
Base configuration for all microservices
Provides common settings and patterns
"""

import os
from typing import List, Dict, Optional, Any, Set
from pydantic_settings import BaseSettings
from pydantic import validator, Field


# ================================================================
# INTERNAL SERVICE REGISTRY
# ================================================================

# Central registry of all internal microservices that should have
# automatic access to tenant resources without user membership
# Service names should match the naming convention used in JWT tokens
INTERNAL_SERVICES: Set[str] = {
    # Core services
    "auth-service",
    "tenant-service",
    "gateway",  # API Gateway
    "gateway-service",  # Alternative name for gateway

    # Business logic services
    "inventory-service",
    "production-service",
    "recipes-service",
    "suppliers-service",
    "pos-service",
    "orders-service",
    "sales-service",
    "procurement-service",

    # ML and analytics services
    "training-service",
    "forecasting-service",
    "ai-insights-service",

    # Orchestration services
    "orchestrator-service",

    # Support services
    "notification-service",
    "alert-service",
    "alert-processor-service",
    "alert-processor",  # Alternative name (from k8s service name)
    "demo-session-service",
    "demo-service",  # Alternative name for demo session service
    "external-service",

    # Enterprise services
    "distribution-service",
}


def is_internal_service(service_identifier: str) -> bool:
    """
    Check if a service identifier represents an internal service.

    Args:
        service_identifier: Service name (e.g., 'production-service')

    Returns:
        bool: True if the identifier is a recognized internal service
    """
    return service_identifier in INTERNAL_SERVICES


class BaseServiceSettings(BaseSettings):
    """
    Base configuration class for all microservices
    Provides common settings and validation patterns
    """
    
    # ================================================================
    # CORE SERVICE SETTINGS
    # ================================================================
    
    # Application Identity
    APP_NAME: str = "Bakery Service"
    SERVICE_NAME: str = "base-service"
    VERSION: str = "1.0.0"
    DESCRIPTION: str = "Base microservice for bakery platform"
    
    # Environment & Debugging
    ENVIRONMENT: str = os.getenv("ENVIRONMENT", "development")
    DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true"
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
    
    # Service Discovery & Health
    SERVICE_HOST: str = os.getenv("SERVICE_HOST", "0.0.0.0")
    SERVICE_PORT: int = int(os.getenv("SERVICE_PORT", "8000"))
    HEALTH_CHECK_ENABLED: bool = True
    METRICS_ENABLED: bool = True
    
    # ================================================================
    # DATABASE CONFIGURATION
    # ================================================================

    # DATABASE_URL as a property - can be overridden by service-specific configs
    # Base implementation reads from environment variable or builds from components
    @property
    def DATABASE_URL(self) -> str:
        """Build database URL from environment or components.

        Service-specific configs should override this property to use their
        own service-specific environment variables (e.g., TENANT_DATABASE_URL).
        """
        # Try complete URL first
        complete_url = os.getenv("DATABASE_URL")
        if complete_url:
            return complete_url

        # Try to build from standard components
        user = os.getenv("DB_USER", "")
        password = os.getenv("DB_PASSWORD", "")
        host = os.getenv("DB_HOST", "localhost")
        port = os.getenv("DB_PORT", "5432")
        name = os.getenv("DB_NAME", "")

        if user and password and name:
            return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"

        # Return empty string if not configured (will fail validation in production)
        return ""

    # Database connection settings
    DB_POOL_SIZE: int = int(os.getenv("DB_POOL_SIZE", "10"))
    DB_MAX_OVERFLOW: int = int(os.getenv("DB_MAX_OVERFLOW", "20"))
    DB_POOL_TIMEOUT: int = int(os.getenv("DB_POOL_TIMEOUT", "30"))
    DB_POOL_RECYCLE: int = int(os.getenv("DB_POOL_RECYCLE", "3600"))
    DB_POOL_PRE_PING: bool = os.getenv("DB_POOL_PRE_PING", "true").lower() == "true"
    DB_ECHO: bool = os.getenv("DB_ECHO", "false").lower() == "true"
    
    # ================================================================
    # REDIS CONFIGURATION
    # ================================================================

    @property
    def REDIS_URL(self) -> str:
        """Build Redis URL from secure components with TLS support"""
        # Try complete URL first (for backward compatibility)
        complete_url = os.getenv("REDIS_URL")
        if complete_url:
            # Upgrade to TLS if not already
            if complete_url.startswith("redis://") and "tls" not in complete_url.lower():
                complete_url = complete_url.replace("redis://", "rediss://", 1)
            return complete_url

        # Build from components (secure approach with TLS)
        password = os.getenv("REDIS_PASSWORD", "")
        host = os.getenv("REDIS_HOST", "redis-service")
        port = os.getenv("REDIS_PORT", "6379")
        use_tls = os.getenv("REDIS_TLS_ENABLED", "true").lower() == "true"

        # Use rediss:// for TLS, redis:// for non-TLS
        protocol = "rediss" if use_tls else "redis"

        # DEBUG: print what we're using
        import sys
        print(f"[DEBUG REDIS_URL] password={repr(password)}, host={host}, port={port}, tls={use_tls}", file=sys.stderr)

        if password:
            url = f"{protocol}://:{password}@{host}:{port}"
            print(f"[DEBUG REDIS_URL] Returning URL with auth and TLS: {url}", file=sys.stderr)
            return url
        url = f"{protocol}://{host}:{port}"
        print(f"[DEBUG REDIS_URL] Returning URL without auth: {url}", file=sys.stderr)
        return url

    REDIS_DB: int = int(os.getenv("REDIS_DB", "0"))
    REDIS_MAX_CONNECTIONS: int = int(os.getenv("REDIS_MAX_CONNECTIONS", "50"))
    REDIS_RETRY_ON_TIMEOUT: bool = True
    REDIS_SOCKET_KEEPALIVE: bool = True
    REDIS_SOCKET_KEEPALIVE_OPTIONS: Dict[str, int] = {
        "TCP_KEEPIDLE": 1,
        "TCP_KEEPINTVL": 3,
        "TCP_KEEPCNT": 5,
    }

    @property
    def REDIS_URL_WITH_DB(self) -> str:
        """Get Redis URL with database number"""
        base_url = self.REDIS_URL.rstrip('/')
        return f"{base_url}/{self.REDIS_DB}"
    
    # ================================================================
    # RABBITMQ CONFIGURATION
    # ================================================================

    @property
    def RABBITMQ_URL(self) -> str:
        """Build RabbitMQ URL from secure components"""
        # Try complete URL first (for backward compatibility)
        complete_url = os.getenv("RABBITMQ_URL")
        if complete_url:
            return complete_url

        # Build from components (secure approach)
        user = os.getenv("RABBITMQ_USER", "bakery")
        password = os.getenv("RABBITMQ_PASSWORD", "forecast123")
        host = os.getenv("RABBITMQ_HOST", "rabbitmq-service")
        port = os.getenv("RABBITMQ_PORT", "5672")
        vhost = os.getenv("RABBITMQ_VHOST", "/")

        return f"amqp://{user}:{password}@{host}:{port}{vhost}"
    RABBITMQ_EXCHANGE: str = os.getenv("RABBITMQ_EXCHANGE", "bakery_events")
    RABBITMQ_QUEUE_PREFIX: str = os.getenv("RABBITMQ_QUEUE_PREFIX", "bakery")
    RABBITMQ_RETRY_ATTEMPTS: int = int(os.getenv("RABBITMQ_RETRY_ATTEMPTS", "3"))
    RABBITMQ_RETRY_DELAY: int = int(os.getenv("RABBITMQ_RETRY_DELAY", "5"))
    
    # ================================================================
    # AUTHENTICATION & SECURITY
    # ================================================================
    
    # JWT Configuration
    # ✅ FIXED: Use production JWT secret key to match auth service
    # Must be same across all services for inter-service communication
    JWT_SECRET_KEY: str = os.getenv("JWT_SECRET_KEY", "usMHw9kQCQoyrc7wPmMi3bClr0lTY9wvzZmcTbADvL0=")
    JWT_ALGORITHM: str = os.getenv("JWT_ALGORITHM", "HS256")
    JWT_ACCESS_TOKEN_EXPIRE_MINUTES: int = int(os.getenv("JWT_ACCESS_TOKEN_EXPIRE_MINUTES", "30"))
    JWT_REFRESH_TOKEN_EXPIRE_DAYS: int = int(os.getenv("JWT_REFRESH_TOKEN_EXPIRE_DAYS", "7"))
    

    
    # Password Requirements
    PASSWORD_MIN_LENGTH: int = int(os.getenv("PASSWORD_MIN_LENGTH", "8"))
    PASSWORD_REQUIRE_UPPERCASE: bool = os.getenv("PASSWORD_REQUIRE_UPPERCASE", "true").lower() == "true"
    PASSWORD_REQUIRE_LOWERCASE: bool = os.getenv("PASSWORD_REQUIRE_LOWERCASE", "true").lower() == "true"
    PASSWORD_REQUIRE_NUMBERS: bool = os.getenv("PASSWORD_REQUIRE_NUMBERS", "true").lower() == "true"
    PASSWORD_REQUIRE_SYMBOLS: bool = os.getenv("PASSWORD_REQUIRE_SYMBOLS", "false").lower() == "true"
    
    # Security Settings
    BCRYPT_ROUNDS: int = int(os.getenv("BCRYPT_ROUNDS", "12"))
    MAX_LOGIN_ATTEMPTS: int = int(os.getenv("MAX_LOGIN_ATTEMPTS", "5"))
    LOCKOUT_DURATION_MINUTES: int = int(os.getenv("LOCKOUT_DURATION_MINUTES", "30"))
    
    # ================================================================
    # INTER-SERVICE COMMUNICATION
    # ================================================================
    
    # Service URLs (can be overridden by environment variables)
    GATEWAY_URL: str = os.getenv("GATEWAY_URL", "http://gateway-service:8000")
    AUTH_SERVICE_URL: str = os.getenv("AUTH_SERVICE_URL", "http://auth-service:8000")
    TRAINING_SERVICE_URL: str = os.getenv("TRAINING_SERVICE_URL", "http://training-service:8000")
    FORECASTING_SERVICE_URL: str = os.getenv("FORECASTING_SERVICE_URL", "http://forecasting-service:8000")
    SALES_SERVICE_URL: str = os.getenv("SALES_SERVICE_URL", "http://sales-service:8000")
    EXTERNAL_SERVICE_URL: str = os.getenv("EXTERNAL_SERVICE_URL", "http://external-service:8000")
    TENANT_SERVICE_URL: str = os.getenv("TENANT_SERVICE_URL", "http://tenant-service:8000")
    INVENTORY_SERVICE_URL: str = os.getenv("INVENTORY_SERVICE_URL", "http://inventory-service:8000")
    NOTIFICATION_SERVICE_URL: str = os.getenv("NOTIFICATION_SERVICE_URL", "http://notification-service:8000")
    PRODUCTION_SERVICE_URL: str = os.getenv("PRODUCTION_SERVICE_URL", "http://bakery-production-service:8000")
    ORDERS_SERVICE_URL: str = os.getenv("ORDERS_SERVICE_URL", "http://bakery-orders-service:8000")
    SUPPLIERS_SERVICE_URL: str = os.getenv("SUPPLIERS_SERVICE_URL", "http://bakery-suppliers-service:8000")
    RECIPES_SERVICE_URL: str = os.getenv("RECIPES_SERVICE_URL", "http://recipes-service:8000")
    POS_SERVICE_URL: str = os.getenv("POS_SERVICE_URL", "http://pos-service:8000")
    NOMINATIM_SERVICE_URL: str = os.getenv("NOMINATIM_SERVICE_URL", "http://nominatim:8080")
    DEMO_SESSION_SERVICE_URL: str = os.getenv("DEMO_SESSION_SERVICE_URL", "http://demo-session-service:8000")
    ALERT_PROCESSOR_SERVICE_URL: str = os.getenv("ALERT_PROCESSOR_SERVICE_URL", "http://alert-processor:8000")
    PROCUREMENT_SERVICE_URL: str = os.getenv("PROCUREMENT_SERVICE_URL", "http://procurement-service:8000")
    ORCHESTRATOR_SERVICE_URL: str = os.getenv("ORCHESTRATOR_SERVICE_URL", "http://orchestrator-service:8000")
    AI_INSIGHTS_SERVICE_URL: str = os.getenv("AI_INSIGHTS_SERVICE_URL", "http://ai-insights-service:8000")
    DISTRIBUTION_SERVICE_URL: str = os.getenv("DISTRIBUTION_SERVICE_URL", "http://distribution-service:8000")

    # HTTP Client Settings
    HTTP_TIMEOUT: int = int(os.getenv("HTTP_TIMEOUT", "30"))
    HTTP_RETRIES: int = int(os.getenv("HTTP_RETRIES", "3"))
    HTTP_RETRY_DELAY: float = float(os.getenv("HTTP_RETRY_DELAY", "1.0"))
    
    # ================================================================
    # CORS & API CONFIGURATION
    # ================================================================
    
    CORS_ORIGINS: str = os.getenv("CORS_ORIGINS", "http://localhost:3000,http://localhost:3001")
    CORS_ALLOW_CREDENTIALS: bool = os.getenv("CORS_ALLOW_CREDENTIALS", "true").lower() == "true"
    CORS_ALLOW_METHODS: List[str] = ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
    CORS_ALLOW_HEADERS: List[str] = ["*"]
    
    @property
    def CORS_ORIGINS_LIST(self) -> List[str]:
        """Get CORS origins as list"""
        return [origin.strip() for origin in self.CORS_ORIGINS.split(",") if origin.strip()]
    
    # Rate Limiting
    RATE_LIMIT_ENABLED: bool = os.getenv("RATE_LIMIT_ENABLED", "true").lower() == "true"
    RATE_LIMIT_REQUESTS: int = int(os.getenv("RATE_LIMIT_REQUESTS", "100"))
    RATE_LIMIT_WINDOW: int = int(os.getenv("RATE_LIMIT_WINDOW", "60"))
    RATE_LIMIT_BURST: int = int(os.getenv("RATE_LIMIT_BURST", "10"))
    
    # API Documentation
    API_DOCS_ENABLED: bool = os.getenv("API_DOCS_ENABLED", "true").lower() == "true"
    API_DOCS_URL: str = "/docs"
    API_REDOC_URL: str = "/redoc"
    API_OPENAPI_URL: str = "/openapi.json"
    
    # ================================================================
    # EXTERNAL APIS & INTEGRATIONS
    # ================================================================
    
    # Weather API (AEMET - Spanish Weather Service)
    AEMET_API_KEY: str = os.getenv("AEMET_API_KEY", "")
    AEMET_BASE_URL: str = "https://opendata.aemet.es/opendata"
    AEMET_TIMEOUT: int = int(os.getenv("AEMET_TIMEOUT", "30"))
    
    # Madrid Open Data
    MADRID_OPENDATA_API_KEY: str = os.getenv("MADRID_OPENDATA_API_KEY", "")
    MADRID_OPENDATA_BASE_URL: str = "https://datos.madrid.es"
    MADRID_OPENDATA_TIMEOUT: int = int(os.getenv("MADRID_OPENDATA_TIMEOUT", "30"))
    
    # Email Configuration
    SMTP_HOST: str = os.getenv("SMTP_HOST", "smtp.gmail.com")
    SMTP_PORT: int = int(os.getenv("SMTP_PORT", "587"))
    SMTP_USER: str = os.getenv("SMTP_USER", "")
    SMTP_PASSWORD: str = os.getenv("SMTP_PASSWORD", "")
    SMTP_TLS: bool = os.getenv("SMTP_TLS", "true").lower() == "true"
    SMTP_SSL: bool = os.getenv("SMTP_SSL", "false").lower() == "true"
    
    # WhatsApp API
    WHATSAPP_API_KEY: str = os.getenv("WHATSAPP_API_KEY", "")
    WHATSAPP_BASE_URL: str = os.getenv("WHATSAPP_BASE_URL", "https://api.twilio.com")
    WHATSAPP_FROM_NUMBER: str = os.getenv("WHATSAPP_FROM_NUMBER", "")
    
    # Stripe Payment Configuration
    STRIPE_PUBLISHABLE_KEY: str = os.getenv("STRIPE_PUBLISHABLE_KEY", "")
    STRIPE_SECRET_KEY: str = os.getenv("STRIPE_SECRET_KEY", "")
    STRIPE_WEBHOOK_SECRET: str = os.getenv("STRIPE_WEBHOOK_SECRET", "")
    STRIPE_API_VERSION: str = os.getenv("STRIPE_API_VERSION", "")  # Empty = use SDK default
    
    # ================================================================
    # ML & AI CONFIGURATION
    # ================================================================

    # Model Storage Backend (MinIO is the primary storage)
    MODEL_STORAGE_BACKEND: str = os.getenv("MODEL_STORAGE_BACKEND", "minio")
    
    # Training Configuration
    MAX_TRAINING_TIME_MINUTES: int = int(os.getenv("MAX_TRAINING_TIME_MINUTES", "30"))
    MIN_TRAINING_DATA_DAYS: int = int(os.getenv("MIN_TRAINING_DATA_DAYS", "30"))
    TRAINING_BATCH_SIZE: int = int(os.getenv("TRAINING_BATCH_SIZE", "1000"))
    
    # Prophet Configuration
    PROPHET_SEASONALITY_MODE: str = os.getenv("PROPHET_SEASONALITY_MODE", "additive")
    PROPHET_CHANGEPOINT_PRIOR_SCALE: float = float(os.getenv("PROPHET_CHANGEPOINT_PRIOR_SCALE", "0.05"))
    PROPHET_SEASONALITY_PRIOR_SCALE: float = float(os.getenv("PROPHET_SEASONALITY_PRIOR_SCALE", "10.0"))
    
    # Prediction Caching
    PREDICTION_CACHE_TTL_HOURS: int = int(os.getenv("PREDICTION_CACHE_TTL_HOURS", "6"))
    WEATHER_CACHE_TTL_HOURS: int = int(os.getenv("WEATHER_CACHE_TTL_HOURS", "1"))
    TRAFFIC_CACHE_TTL_HOURS: int = int(os.getenv("TRAFFIC_CACHE_TTL_HOURS", "1"))
    
    # ================================================================
    # MONITORING & OBSERVABILITY
    # ================================================================
    
    # Logging Configuration
    LOG_FORMAT: str = os.getenv("LOG_FORMAT", "json")  # json, text
    LOG_FILE_ENABLED: bool = os.getenv("LOG_FILE_ENABLED", "false").lower() == "true"
    LOG_FILE_PATH: str = os.getenv("LOG_FILE_PATH", "/app/logs")
    LOG_ROTATION_SIZE: str = os.getenv("LOG_ROTATION_SIZE", "100MB")
    LOG_RETENTION_DAYS: int = int(os.getenv("LOG_RETENTION_DAYS", "30"))
    
    # Metrics & Monitoring
    PROMETHEUS_ENABLED: bool = os.getenv("PROMETHEUS_ENABLED", "true").lower() == "true"
    PROMETHEUS_PORT: int = int(os.getenv("PROMETHEUS_PORT", "9090"))
    PROMETHEUS_PATH: str = "/metrics"
    
    # Tracing
    JAEGER_ENABLED: bool = os.getenv("JAEGER_ENABLED", "false").lower() == "true"
    JAEGER_AGENT_HOST: str = os.getenv("JAEGER_AGENT_HOST", "localhost")
    JAEGER_AGENT_PORT: int = int(os.getenv("JAEGER_AGENT_PORT", "6831"))
    
    # Health Checks
    HEALTH_CHECK_TIMEOUT: int = int(os.getenv("HEALTH_CHECK_TIMEOUT", "30"))
    HEALTH_CHECK_INTERVAL: int = int(os.getenv("HEALTH_CHECK_INTERVAL", "30"))
    
    # ================================================================
    # DATA RETENTION & CLEANUP
    # ================================================================
    
    DATA_RETENTION_DAYS: int = int(os.getenv("DATA_RETENTION_DAYS", "365"))
    LOG_RETENTION_DAYS: int = int(os.getenv("LOG_RETENTION_DAYS", "90"))
    METRIC_RETENTION_DAYS: int = int(os.getenv("METRIC_RETENTION_DAYS", "90"))
    TEMP_FILE_CLEANUP_HOURS: int = int(os.getenv("TEMP_FILE_CLEANUP_HOURS", "24"))
    
    # ================================================================
    # BUSINESS RULES & CONSTRAINTS
    # ================================================================
    
    # Forecasting Business Rules
    MAX_FORECAST_DAYS: int = int(os.getenv("MAX_FORECAST_DAYS", "30"))
    MIN_HISTORICAL_DAYS: int = int(os.getenv("MIN_HISTORICAL_DAYS", "60"))
    CONFIDENCE_THRESHOLD: float = float(os.getenv("CONFIDENCE_THRESHOLD", "0.8"))
    
    # Spanish Business Context
    TIMEZONE: str = os.getenv("TIMEZONE", "Europe/Madrid")
    LOCALE: str = os.getenv("LOCALE", "es_ES.UTF-8")
    CURRENCY: str = os.getenv("CURRENCY", "EUR")
    
    # Business Hours (24-hour format)
    BUSINESS_HOUR_START: int = int(os.getenv("BUSINESS_HOUR_START", "7"))
    BUSINESS_HOUR_END: int = int(os.getenv("BUSINESS_HOUR_END", "20"))
    
    # Spanish Holidays & Seasonal Adjustments
    ENABLE_SPANISH_HOLIDAYS: bool = os.getenv("ENABLE_SPANISH_HOLIDAYS", "true").lower() == "true"
    ENABLE_MADRID_HOLIDAYS: bool = os.getenv("ENABLE_MADRID_HOLIDAYS", "true").lower() == "true"
    SCHOOL_CALENDAR_ENABLED: bool = os.getenv("SCHOOL_CALENDAR_ENABLED", "true").lower() == "true"

    # ================================================================
    # PROCUREMENT AUTOMATION
    # ================================================================

    # NOTE: Tenant-specific procurement settings (auto-approval thresholds, supplier scores,
    # approval rules, lead times, forecast days, etc.) have been moved to TenantSettings.
    # Services should fetch these using TenantSettingsClient from shared/utils/tenant_settings_client.py

    # System-level procurement settings (apply to all tenants):
    AUTO_CREATE_POS_FROM_PLAN: bool = os.getenv("AUTO_CREATE_POS_FROM_PLAN", "true").lower() == "true"
    PROCUREMENT_TEST_MODE: bool = os.getenv("PROCUREMENT_TEST_MODE", "false").lower() == "true"
    SEND_AUTO_APPROVAL_SUMMARY: bool = os.getenv("SEND_AUTO_APPROVAL_SUMMARY", "true").lower() == "true"
    AUTO_APPROVAL_SUMMARY_TIME_HOUR: int = int(os.getenv("AUTO_APPROVAL_SUMMARY_TIME_HOUR", "18"))

    # ================================================================
    # DEVELOPMENT & TESTING
    # ================================================================
    
    # Testing Configuration
    TESTING: bool = os.getenv("TESTING", "false").lower() == "true"
    TEST_DATABASE_URL: str = os.getenv("TEST_DATABASE_URL", "")
    MOCK_EXTERNAL_APIS: bool = os.getenv("MOCK_EXTERNAL_APIS", "false").lower() == "true"
    
    # Development Features
    AUTO_RELOAD: bool = os.getenv("AUTO_RELOAD", "false").lower() == "true"
    PROFILING_ENABLED: bool = os.getenv("PROFILING_ENABLED", "false").lower() == "true"
    
    # ================================================================
    # VALIDATORS
    # ================================================================
    
    @validator('JWT_SECRET_KEY')
    def validate_jwt_secret(cls, v):
        if v == "change-this-in-production" and os.getenv("ENVIRONMENT") == "production":
            raise ValueError("JWT_SECRET_KEY must be changed in production")
        if len(v) < 32:
            raise ValueError("JWT_SECRET_KEY must be at least 32 characters long")
        return v
    
    @validator('LOG_LEVEL')
    def validate_log_level(cls, v):
        valid_levels = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
        if v.upper() not in valid_levels:
            raise ValueError(f"LOG_LEVEL must be one of: {valid_levels}")
        return v.upper()
    
    @validator('ENVIRONMENT')
    def validate_environment(cls, v):
        valid_envs = ['development', 'staging', 'production', 'testing']
        if v.lower() not in valid_envs:
            raise ValueError(f"ENVIRONMENT must be one of: {valid_envs}")
        return v.lower()
    
    # ================================================================
    # COMPUTED PROPERTIES
    # ================================================================
    
    @property
    def IS_PRODUCTION(self) -> bool:
        """Check if running in production"""
        return self.ENVIRONMENT == "production"
    
    @property
    def IS_DEVELOPMENT(self) -> bool:
        """Check if running in development"""
        return self.ENVIRONMENT == "development"
    
    @property
    def IS_TESTING(self) -> bool:
        """Check if running tests"""
        return self.TESTING or self.ENVIRONMENT == "testing"
    
    @property
    def SERVICE_REGISTRY(self) -> Dict[str, str]:
        """Get all service URLs"""
        return {
            "gateway": self.GATEWAY_URL,
            "auth": self.AUTH_SERVICE_URL,
            "training": self.TRAINING_SERVICE_URL,
            "forecasting": self.FORECASTING_SERVICE_URL,
            "sales": self.SALES_SERVICE_URL,
            "external": self.EXTERNAL_SERVICE_URL,
            "tenant": self.TENANT_SERVICE_URL,
            "inventory": self.INVENTORY_SERVICE_URL,
            "notification": self.NOTIFICATION_SERVICE_URL,
            "production": self.PRODUCTION_SERVICE_URL,
            "orders": self.ORDERS_SERVICE_URL,
            "suppliers": self.SUPPLIERS_SERVICE_URL,
            "recipes": self.RECIPES_SERVICE_URL,
        }
    
    @property
    def DATABASE_CONFIG(self) -> Dict[str, Any]:
        """Get database configuration for SQLAlchemy"""
        return {
            "url": self.DATABASE_URL,
            "pool_size": self.DB_POOL_SIZE,
            "max_overflow": self.DB_MAX_OVERFLOW,
            "pool_timeout": self.DB_POOL_TIMEOUT,
            "pool_recycle": self.DB_POOL_RECYCLE,
            "pool_pre_ping": self.DB_POOL_PRE_PING,
            "echo": self.DB_ECHO,
        }
    
    @property
    def REDIS_CONFIG(self) -> Dict[str, Any]:
        """Get Redis configuration"""
        return {
            "url": self.REDIS_URL_WITH_DB,
            "max_connections": self.REDIS_MAX_CONNECTIONS,
            "retry_on_timeout": self.REDIS_RETRY_ON_TIMEOUT,
            "socket_keepalive": self.REDIS_SOCKET_KEEPALIVE,
            "socket_keepalive_options": self.REDIS_SOCKET_KEEPALIVE_OPTIONS,
        }
    
    # ================================================================
    # CONFIGURATION LOADING
    # ================================================================
    
    class Config:
        env_file = ".env"
        env_file_encoding = 'utf-8'
        case_sensitive = True
        
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        
        # Validate critical settings in production
        if self.IS_PRODUCTION:
            self._validate_production_settings()
    
    def _validate_production_settings(self):
        """Validate production-specific settings"""
        critical_settings = [
            'JWT_SECRET_KEY',
            'DATABASE_URL',
            'REDIS_URL',
            'RABBITMQ_URL'
        ]
        
        for setting in critical_settings:
            value = getattr(self, setting)
            if not value or 'change' in value.lower() or 'default' in value.lower():
                raise ValueError(f"{setting} must be properly configured for production")
