"""Configuration management for Armory SDK."""

import os
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class ArmoryConfig:
    """Armory SDK configuration."""

    # Server connection
    server_url: str = "http://localhost:8080"
    api_key: Optional[str] = None

    # Auto-features
    auto_retrospective: bool = True  # Auto-call retrospective on session end
    auto_skill_creation: bool = True  # Auto-create skill from successful experiments
    auto_log_system_metrics: bool = True  # Auto-log GPU/CPU metrics

    # Thresholds for auto-skill creation
    min_success_metric: float = 0.0  # Minimum metric value to consider success
    success_metric_name: str = "accuracy"  # Which metric determines success

    # Retry settings
    max_retries: int = 3
    retry_delay: float = 1.0

    # Logging
    verbose: bool = False

    @classmethod
    def from_env(cls) -> "ArmoryConfig":
        """Create config from environment variables."""
        return cls(
            server_url=os.getenv("ARMORY_SERVER_URL", "http://localhost:8080"),
            api_key=os.getenv("ARMORY_API_KEY"),
            auto_retrospective=os.getenv("ARMORY_AUTO_RETROSPECTIVE", "true").lower() == "true",
            auto_skill_creation=os.getenv("ARMORY_AUTO_SKILL_CREATION", "true").lower() == "true",
            auto_log_system_metrics=os.getenv("ARMORY_AUTO_SYSTEM_METRICS", "true").lower()
            == "true",
            min_success_metric=float(os.getenv("ARMORY_MIN_SUCCESS_METRIC", "0.0")),
            success_metric_name=os.getenv("ARMORY_SUCCESS_METRIC_NAME", "accuracy"),
            verbose=os.getenv("ARMORY_VERBOSE", "false").lower() == "true",
        )


# Global config instance
_config: Optional[ArmoryConfig] = None


def get_config() -> ArmoryConfig:
    """Get the global Armory configuration."""
    global _config
    if _config is None:
        _config = ArmoryConfig.from_env()
    return _config


def configure(
    server_url: Optional[str] = None,
    api_key: Optional[str] = None,
    auto_retrospective: Optional[bool] = None,
    auto_skill_creation: Optional[bool] = None,
    auto_log_system_metrics: Optional[bool] = None,
    min_success_metric: Optional[float] = None,
    success_metric_name: Optional[str] = None,
    verbose: Optional[bool] = None,
) -> ArmoryConfig:
    """Configure the Armory SDK.

    Args:
        server_url: Armory server URL
        api_key: API key for authentication
        auto_retrospective: Auto-call retrospective on session end
        auto_skill_creation: Auto-create skill from successful experiments
        auto_log_system_metrics: Auto-log GPU/CPU metrics
        min_success_metric: Minimum metric value to consider success
        success_metric_name: Which metric determines success
        verbose: Enable verbose logging

    Returns:
        Updated configuration
    """
    global _config
    config = get_config()

    if server_url is not None:
        config.server_url = server_url
    if api_key is not None:
        config.api_key = api_key
    if auto_retrospective is not None:
        config.auto_retrospective = auto_retrospective
    if auto_skill_creation is not None:
        config.auto_skill_creation = auto_skill_creation
    if auto_log_system_metrics is not None:
        config.auto_log_system_metrics = auto_log_system_metrics
    if min_success_metric is not None:
        config.min_success_metric = min_success_metric
    if success_metric_name is not None:
        config.success_metric_name = success_metric_name
    if verbose is not None:
        config.verbose = verbose

    _config = config
    return config
