#!/usr/bin/env python3
"""Check arithmetic in the reported aggregates. Does not read or collect chats."""
import json
from pathlib import Path

data = json.loads(Path(__file__).with_name("aggregates.json").read_text())
c = data["counts"]
n = c["available_conversations"]

if c["share_links_found"] != n + c["unavailable_links"]:
    raise ValueError("The available and unavailable counts do not add up.")
if not 0 <= c["tool_conversations_starting_before_message_3"] <= c["conversations_with_tools"] <= n:
    raise ValueError("The tool-use counts are inconsistent.")
if not 0 <= c["provisional_conversations_with_user_pasted_credentials"] <= n:
    raise ValueError("The provisional credential count is inconsistent.")
if not 0 <= c["conversations_with_more_than_50_messages"] <= c["conversations_with_at_least_50_messages"] <= n:
    raise ValueError("The conversation-length counts are inconsistent.")

ratios = {
    "unavailable_share_links": (c["unavailable_links"], c["share_links_found"]),
    "conversations_with_tools": (c["conversations_with_tools"], n),
    "early_tool_use_among_tool_conversations": (
        c["tool_conversations_starting_before_message_3"], c["conversations_with_tools"]
    ),
    "provisional_user_pasted_credential_rate": (
        c["provisional_conversations_with_user_pasted_credentials"], n
    ),
    "english_conversations": (c["english_conversations"], n),
}
print(json.dumps({
    "scope": "Arithmetic only; collection and classifications are not reproduced",
    "percentages": {
        label: {"numerator": a, "denominator": b, "percent": round(100 * a / b, 4)}
        for label, (a, b) in ratios.items()
    },
    "credential_rate_one_in": round(n / c["provisional_conversations_with_user_pasted_credentials"], 2),
}, indent=2))
