"""
Utility functions for BrighterTrading.
"""
import math
import numpy as np


def sanitize_for_json(value):
    """
    Convert a value to a JSON-serializable type.
    Handles numpy types, inf, nan, and nested structures.

    :param value: Any value that needs to be JSON serialized
    :return: JSON-serializable value
    """
    # Handle None
    if value is None:
        return None
    # Handle numpy integer types
    if isinstance(value, np.integer):
        return int(value)
    # Handle numpy floating types
    elif isinstance(value, np.floating):
        float_val = float(value)
        if math.isinf(float_val) or math.isnan(float_val):
            return None
        return float_val
    # Handle numpy boolean
    elif isinstance(value, np.bool_):
        return bool(value)
    # Handle numpy arrays
    elif isinstance(value, np.ndarray):
        return [sanitize_for_json(v) for v in value.tolist()]
    # Handle regular floats with inf/nan
    elif isinstance(value, float):
        if math.isinf(value) or math.isnan(value):
            return None
        return value
    # Handle nested dicts
    elif isinstance(value, dict):
        return {k: sanitize_for_json(v) for k, v in value.items()}
    # Handle lists
    elif isinstance(value, list):
        return [sanitize_for_json(v) for v in value]
    # Handle tuples
    elif isinstance(value, tuple):
        return tuple(sanitize_for_json(v) for v in value)
    # Return other types as-is (str, int, bool, etc.)
    return value


def sanitize_dict_for_json(data: dict) -> dict:
    """
    Sanitize a dictionary for JSON serialization.
    Convenience wrapper around sanitize_for_json for dictionaries.

    :param data: Dictionary to sanitize
    :return: JSON-serializable dictionary
    """
    return sanitize_for_json(data)
