"""Summarize the published aggregate only; no network or raw-log access."""

import csv
import json
from collections import Counter
from pathlib import Path


def summarize(path):
    matched = Counter()
    claimed_only = Counter()
    with Path(path).open(newline="", encoding="utf-8") as stream:
        rows = csv.DictReader(stream)
        if rows.fieldnames != ["agent", "identity_check", "http_status", "requests"]:
            raise ValueError("Unexpected aggregate columns")
        for row in rows:
            count = int(row["requests"])
            status = int(row["http_status"])
            if count < 0 or not 100 <= status <= 599:
                raise ValueError("Invalid aggregate count or HTTP status")
            key = (row["agent"], status)
            if row["identity_check"] == "matched_published_ranges":
                matched[key] += count
            elif row["identity_check"] == "agent_claim_only":
                claimed_only[key] += count
            else:
                raise ValueError("Unknown identity classification")
    return {
        "matched_request_records": sum(matched.values()),
        "claimed_only_request_records": sum(claimed_only.values()),
        "all_selected_request_records": sum(matched.values()) + sum(claimed_only.values()),
        "matched_by_agent": {
            agent: sum(count for (name, _), count in matched.items() if name == agent)
            for agent in sorted({agent for agent, _ in matched})
        },
        "matched_by_status": {
            str(status): sum(count for (_, code), count in matched.items() if code == status)
            for status in sorted({status for _, status in matched})
        },
        "meaning": "Request records, not citations, readers, clicks, or ranking gains",
    }


if __name__ == "__main__":
    aggregate = Path(__file__).with_name("ithy-ai-crawler-observations-20261024.csv")
    print(json.dumps(summarize(aggregate), indent=2, sort_keys=True))
