#!/usr/bin/env python3
"""Validate deterministic Chapter 9 streaming-window output."""

from __future__ import annotations

import argparse
import csv
from pathlib import Path


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Compare actual Chapter 9 lab output with an expected CSV.")
    parser.add_argument("--actual", required=True, help="CSV produced by streaming_window_simulator.py.")
    parser.add_argument("--expected", required=True, help="Expected CSV file.")
    return parser.parse_args()


def read_csv(path: Path) -> list[dict[str, str]]:
    if not path.exists():
        raise SystemExit(f"VALIDATION FAILED: missing file {path}")
    with path.open("r", encoding="utf-8", newline="") as f:
        return list(csv.DictReader(f))


def main() -> None:
    args = parse_args()
    actual_path = Path(args.actual)
    expected_path = Path(args.expected)
    actual = read_csv(actual_path)
    expected = read_csv(expected_path)
    if actual != expected:
        print("VALIDATION FAILED: actual output does not match expected output")
        print(f"Actual rows ({len(actual)}): {actual!r}")
        print(f"Expected rows ({len(expected)}): {expected!r}")
        raise SystemExit(1)
    print(f"VALIDATION PASSED: {actual_path} matches {expected_path} ({len(actual)} rows)")


if __name__ == "__main__":
    main()
