# ================================================================
# TRAINING SERVICE CONFIGURATION
# services/training/app/core/config.py
# ================================================================

"""
Training service configuration
ML model training and management
"""

from shared.config.base import BaseServiceSettings
import os

class TrainingSettings(BaseServiceSettings):
    """Training service specific settings"""
    
    # Service Identity
    APP_NAME: str = "Training Service"
    SERVICE_NAME: str = "training-service"
    DESCRIPTION: str = "Machine learning model training service"
    
    # 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("TRAINING_DATABASE_URL")
        if complete_url:
            return complete_url

        # Build from components (secure approach)
        user = os.getenv("TRAINING_DB_USER", "training_user")
        password = os.getenv("TRAINING_DB_PASSWORD", "training_pass123")
        host = os.getenv("TRAINING_DB_HOST", "localhost")
        port = os.getenv("TRAINING_DB_PORT", "5432")
        name = os.getenv("TRAINING_DB_NAME", "training_db")

        return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"
    
    # Redis Database (dedicated for training cache)
    REDIS_DB: int = 1
    
    # ML Model Storage
    MODEL_BACKUP_ENABLED: bool = os.getenv("MODEL_BACKUP_ENABLED", "true").lower() == "true"
    MODEL_VERSIONING_ENABLED: bool = os.getenv("MODEL_VERSIONING_ENABLED", "true").lower() == "true"
    
    # MinIO Configuration
    MINIO_ENDPOINT: str = os.getenv("MINIO_ENDPOINT", "minio.bakery-ia.svc.cluster.local:9000")
    MINIO_ACCESS_KEY: str = os.getenv("MINIO_ACCESS_KEY", "training-service")
    MINIO_SECRET_KEY: str = os.getenv("MINIO_SECRET_KEY", "training-secret-key")
    MINIO_USE_SSL: bool = os.getenv("MINIO_USE_SSL", "true").lower() == "true"
    MINIO_MODEL_BUCKET: str = os.getenv("MINIO_MODEL_BUCKET", "training-models")
    MINIO_CONSOLE_PORT: str = os.getenv("MINIO_CONSOLE_PORT", "9001")
    MINIO_API_PORT: str = os.getenv("MINIO_API_PORT", "9000")
    MINIO_REGION: str = os.getenv("MINIO_REGION", "us-east-1")
    MINIO_MODEL_LIFECYCLE_DAYS: int = int(os.getenv("MINIO_MODEL_LIFECYCLE_DAYS", "90"))
    MINIO_CACHE_TTL_SECONDS: int = int(os.getenv("MINIO_CACHE_TTL_SECONDS", "3600"))
    
    # Training Configuration
    MAX_CONCURRENT_TRAINING_JOBS: int = int(os.getenv("MAX_CONCURRENT_TRAINING_JOBS", "3"))
    
    # Prophet Specific Configuration
    PROPHET_HOLIDAYS_PRIOR_SCALE: float = float(os.getenv("PROPHET_HOLIDAYS_PRIOR_SCALE", "10.0"))
    
    # Spanish Holiday Integration
    ENABLE_CUSTOM_HOLIDAYS: bool = os.getenv("ENABLE_CUSTOM_HOLIDAYS", "true").lower() == "true"
    
    # Data Processing
    DATA_PREPROCESSING_ENABLED: bool = True
    OUTLIER_DETECTION_ENABLED: bool = os.getenv("OUTLIER_DETECTION_ENABLED", "true").lower() == "true"
    SEASONAL_DECOMPOSITION_ENABLED: bool = os.getenv("SEASONAL_DECOMPOSITION_ENABLED", "true").lower() == "true"
    
    # Model Validation
    CROSS_VALIDATION_ENABLED: bool = os.getenv("CROSS_VALIDATION_ENABLED", "true").lower() == "true"
    VALIDATION_SPLIT_RATIO: float = float(os.getenv("VALIDATION_SPLIT_RATIO", "0.2"))
    MIN_MODEL_ACCURACY: float = float(os.getenv("MIN_MODEL_ACCURACY", "0.7"))
    
    # Distributed Training (for future scaling)
    DISTRIBUTED_TRAINING_ENABLED: bool = os.getenv("DISTRIBUTED_TRAINING_ENABLED", "false").lower() == "true"
    TRAINING_WORKER_COUNT: int = int(os.getenv("TRAINING_WORKER_COUNT", "1"))
    
    PROPHET_DAILY_SEASONALITY: bool = True
    PROPHET_WEEKLY_SEASONALITY: bool = True
    PROPHET_YEARLY_SEASONALITY: bool = True

    # Throttling settings for parallel training to prevent heartbeat blocking
    MAX_CONCURRENT_TRAININGS: int = int(os.getenv("MAX_CONCURRENT_TRAININGS", "3"))

settings = TrainingSettings()
