# /// script
# requires-python = ">=3.10"
# ///
# Copyright (c) 2026 Aayush Sharma · SPDX-License-Identifier: MIT
# https://thedataengineering.com/lab-code/LICENSE.txt
"""Trigger mode against sink.

A window fires twice: an early total of 3,100, then the final 3,260. The
firings are emitted in accumulating mode (each firing carries the full total)
or discarding mode (each carries only what is new: 3,100, then +160), into a
sink that overwrites by window key or one that adds.
"""
import argparse, json

p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
p.add_argument("--json", help="Also write the results to this JSON file.")
args = p.parse_args()

firings = {"accumulating": [3100, 3260], "discarding": [3100, 160]}
rows = []
for mode, values in firings.items():
    for sink in ("overwrite by window key", "additive counter"):
        table = {}
        for v in values:
            if sink.startswith("overwrite"): table["w1"] = v
            else: table["w1"] = table.get("w1", 0) + v
        rows.append({"mode": mode, "sink": sink, "result": table["w1"], "correct": "yes" if table["w1"] == 3260 else "no"})
for r in rows: print(f"{r['mode']:<13} into {r['sink']:<24} -> {r['result']:>5}  correct: {r['correct']}")

if args.json:
    with open(args.json, "w") as f:
        json.dump({"tables": {"combinations": rows}}, f)
