"""
Auto-detecting configuration for DataManager.

Provides intelligent defaults based on system capabilities.
"""

import psutil
from dataclasses import dataclass
from typing import Optional


@dataclass
class DataManagerConfig:
    """Auto-detecting configuration for data operations"""

    memory_limit_gb: float
    storage_policy: str  # "conservative", "balanced", "aggressive"
    processing_preference: str  # "speed", "memory", "balanced"
    chunk_size_mb: float
    compression_enabled: bool

    @classmethod
    def auto_detect(cls) -> "DataManagerConfig":
        """Create optimal configuration based on system capabilities"""
        # Auto-detect memory (80% of available)
        available_memory = psutil.virtual_memory().total / (1024**3)
        memory_limit = max(2.0, available_memory * 0.8)

        # Auto-detect storage policy based on disk space
        disk_space = psutil.disk_usage("/").free / (1024**3)
        if disk_space > 50:
            storage_policy = "conservative"
        elif disk_space > 20:
            storage_policy = "balanced"
        else:
            storage_policy = "aggressive"

        # Auto-detect processing preference
        cpu_count = psutil.cpu_count()
        if memory_limit > 8 and cpu_count > 4:
            processing_preference = "speed"
        elif memory_limit < 4:
            processing_preference = "memory"
        else:
            processing_preference = "balanced"

        return cls(
            memory_limit_gb=memory_limit,
            storage_policy=storage_policy,
            processing_preference=processing_preference,
            chunk_size_mb=min(500.0, memory_limit * 100),  # 100MB per GB of memory
            compression_enabled=storage_policy in ["balanced", "aggressive"],
        )

    def summary(self) -> str:
        """Human-readable configuration summary"""
        return (
            f"Memory: {self.memory_limit_gb:.1f}GB, "
            f"Storage: {self.storage_policy}, "
            f"Processing: {self.processing_preference}"
        )


def create_auto_config() -> DataManagerConfig:
    """Create auto-detecting configuration"""
    return DataManagerConfig.auto_detect()
