#!/usr/bin/env python3
"""Verify published aggregate rows for desk log KB-MARGIN-20260822.

This script checks the first-party CSV only. It does not reconstruct the
14,000-row sanitized composite and is not an independent reproduction.
"""

from __future__ import annotations

import csv
import pathlib
import sys

EXPECTED = [
    {
        "retrieval_state": "unbound",
        "sales_margin_pct": "24.0",
        "finance_margin_pct": "24.0",
        "ops_margin_pct": "24.0",
        "sql_artifacts": "1",
        "memo_artifacts": "0",
        "chart_artifacts": "0",
        "csv_artifacts": "0",
        "retrieved_passages": "0",
    },
    {
        "retrieval_state": "bound",
        "sales_margin_pct": "24.0",
        "finance_margin_pct": "18.5",
        "ops_margin_pct": "21.0",
        "sql_artifacts": "1",
        "memo_artifacts": "1",
        "chart_artifacts": "2",
        "csv_artifacts": "1",
        "retrieved_passages": "3",
    },
]


def main() -> int:
    csv_path = pathlib.Path(__file__).with_name(
        "aggregate-KB-MARGIN-20260822.csv"
    )
    with csv_path.open(newline="", encoding="utf-8") as handle:
        rows = list(csv.DictReader(handle))
    if rows != EXPECTED:
        print("FAIL: aggregate rows do not match the desk log.", file=sys.stderr)
        return 1
    print("OK: aggregate rows match desk log KB-MARGIN-20260822.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
