import json
from statistics import mean, median


def parse_jsonish(value):
    """Parse nested JSON strings until a non-string value is reached."""
    current = value
    while isinstance(current, str):
        text = current.strip()
        if not text:
            return current
        try:
            current = json.loads(text)
        except json.JSONDecodeError:
            return current
    return current


def safe_int(value, default=0):
    if value is None or value == "":
        return default
    try:
        return int(value)
    except (TypeError, ValueError):
        return default


def safe_float(value, default=0.0):
    if value is None or value == "":
        return default
    try:
        return float(value)
    except (TypeError, ValueError):
        return default


def percentile(values, pct):
    if not values:
        return 0.0
    ordered = sorted(values)
    if len(ordered) == 1:
        return float(ordered[0])
    rank = pct * (len(ordered) - 1)
    low = int(rank)
    high = min(low + 1, len(ordered) - 1)
    fraction = rank - low
    return ordered[low] + (ordered[high] - ordered[low]) * fraction


def series_stats(values):
    cleaned = [v for v in values if v is not None]
    if not cleaned:
        return {
            "count": 0,
            "min": 0,
            "max": 0,
            "mean": 0.0,
            "median": 0.0,
            "p90": 0.0,
        }
    return {
        "count": len(cleaned),
        "min": min(cleaned),
        "max": max(cleaned),
        "mean": mean(cleaned),
        "median": median(cleaned),
        "p90": percentile(cleaned, 0.9),
    }


def safe_div(numerator, denominator):
    if not denominator:
        return 0.0
    return numerator / denominator


def compact_json(data):
    return json.dumps(data, ensure_ascii=False, separators=(",", ":"))
