#!/usr/bin/env python3
"""Offline integration examples; no network, credentials, or file writes.

Review this code before running it. Wire envelopes below are fixtures, not
captures of real client/server traffic. Observations apply only to these inputs.
"""
import hashlib
import json
import platform
import unicodedata


def sha256(value):
    return hashlib.sha256(value).hexdigest()


def classify_tool_response(status, envelope):
    """Classify these complete tools/call response fixtures, not arbitrary MCP."""
    if not 200 <= status < 300:
        return "http_error"
    if "error" in envelope:
        return "protocol_error"
    if "result" not in envelope:
        return "invalid_response"
    if envelope["result"].get("isError") is True:
        return "tool_error"
    return "success"


raw = '{ "label": "مرحبا", "values": [11, 22, 33] }\n'.encode("utf-8")
parsed = json.loads(raw)
reserialized = json.dumps(parsed, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
assert json.loads(reserialized) == parsed
assert sha256(raw) != sha256(reserialized)
assert parsed["label"] == "مرحبا"

composed = "caf\u00e9"
decomposed = "cafe\u0301"
assert composed != decomposed
assert unicodedata.normalize("NFC", composed) == unicodedata.normalize("NFC", decomposed)
assert sha256(composed.encode()) != sha256(decomposed.encode())

fixtures = {
    "success": (200, {"jsonrpc": "2.0", "id": 1, "result": {"content": [], "isError": False}}),
    "tool_error": (200, {"jsonrpc": "2.0", "id": 1, "result": {"content": [{"type": "text", "text": "Missing input"}], "isError": True}}),
    "protocol_error": (200, {"jsonrpc": "2.0", "id": 1, "error": {"code": -32602, "message": "Invalid params"}}),
    "http_error": (401, {"message": "Unauthorized"}),
}
outcomes = {name: classify_tool_response(*value) for name, value in fixtures.items()}
assert all(name == outcome for name, outcome in outcomes.items())

print(json.dumps({
    "python": platform.python_version(),
    "unicode_database": unicodedata.unidata_version,
    "scope": "Offline fixed-input examples; no live MCP/client interoperability test.",
    "artifact_bytes": {
        "raw_sha256": sha256(raw), "reserialized_sha256": sha256(reserialized),
        "json_values_equal": parsed == json.loads(reserialized), "arabic_label": parsed["label"],
        "count": len(parsed["values"]), "sum": sum(parsed["values"]),
        "mean": sum(parsed["values"]) / len(parsed["values"]),
    },
    "unicode": {
        "composed_codepoints": [f"U+{ord(c):04X}" for c in composed],
        "decomposed_codepoints": [f"U+{ord(c):04X}" for c in decomposed],
        "raw_hashes_equal": False, "nfc_equal": True,
    },
    "response_fixtures": outcomes,
}, ensure_ascii=False, indent=2))
