# /// script
# requires-python = ">=3.10"
# ///
# Copyright (c) 2026 Aayush Sharma · SPDX-License-Identifier: MIT
# https://thedataengineering.com/lab-code/LICENSE.txt
"""A reader-side default rewrites history.

1,000 stored events with no delivery_priority field, counted by value under a
reader schema whose default is STANDARD, then EXPRESS, without touching the
data. Then the same with the default written into the data at write time.
Also checks which of the three states (absent, null, defaulted) each approach
can still tell apart.
"""
import argparse, json, random
from collections import Counter

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

stored_old = [{"id": i} for i in range(args.events)]                     # written before the field existed
def read(stored, default):
    return Counter(e.get("delivery_priority", default) for e in stored)  # the reader fills in the default
def write(default): return [{"id": i, "delivery_priority": default} for i in range(args.events)]   # the writer stores it
rows = []
for d in ("STANDARD", "EXPRESS"):
    c = read(stored_old, d); rows.append({"approach": "reader-side default", "default_now": d, "STANDARD": c.get("STANDARD", 0), "EXPRESS": c.get("EXPRESS", 0)})
stored_new = write("STANDARD")                                           # the default as it was at write time
for d in ("STANDARD", "EXPRESS"):
    c = read(stored_new, d); rows.append({"approach": "writer-side default", "default_now": d, "STANDARD": c.get("STANDARD", 0), "EXPRESS": c.get("EXPRESS", 0)})
mixed = [{"id": 1}, {"id": 2, "delivery_priority": None}, {"id": 3, "delivery_priority": "STANDARD"}]
states = [{"approach": "reader-side default", "absent_vs_defaulted": "no", "null_vs_defaulted": "yes",
           "seen": ", ".join(str(e.get("delivery_priority", "STANDARD")) for e in mixed)},
          {"approach": "writer-side default", "absent_vs_defaulted": "only if absent rows predate the field", "null_vs_defaulted": "yes",
           "seen": ", ".join(str(e.get("delivery_priority", "<absent>")) for e in mixed)}]
for r in rows: print(f"{r['approach']:<20} default {r['default_now']:<9} STANDARD {r['STANDARD']:>5}  EXPRESS {r['EXPRESS']:>5}")
for s in states: print(f"{s['approach']:<20} absent / null / defaulted reads as: {s['seen']}")

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