from __future__ import annotations

import json
import sys
from datetime import datetime, timezone
from typing import Any


def audit_log(entry: dict[str, Any]) -> None:
    record = {"ts": datetime.now(timezone.utc).isoformat(), **entry}
    print(json.dumps(record, default=str), file=sys.stderr, flush=True)


def sanitize_args(tool_name: str, args: dict[str, Any]) -> dict[str, Any]:
    """Sanitize tool arguments for logging — never log tokens or large payloads."""
    safe: dict[str, Any] = {}
    for key, value in args.items():
        if key == "content" and isinstance(value, str):
            safe["contentSizeBytes"] = len(value) * 3 // 4
            continue
        if isinstance(value, str) and len(value) > 200:
            safe[key] = f"{value[:50]}...[{len(value)} chars]"
            continue
        safe[key] = value
    return safe
