"""Does the citizen ever *read* the notice PR #4 writes for them?

PR #4 makes a turn survive a thing whose `state_json` is not JSON, and writes
the affected citizen a notice saying the failure was the engine's fault and
their action points were not touched. That notice is delivered in the turn
report. The report is rendered by `reports.build`, and `reports._carried`
filters on `json_extract(t.state_json, '$.carried_by')` with no tile filter and
no owner filter — which is H72.

So this asks the question PR #4 cannot answer about itself: with the malformed
row present, does the report that carries the apology render at all?

    cd /home/vesper/pr4wt && python research/hesper/pr4-review/report_delivery.py
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[3]))

from hesper import db, reports
from hesper.core import loader
from broken_tent_turn import world, TURN   # same fixture, same broken tent


def main():
    for broken in (False, True):
        tmp, conn = world(broken)
        try:
            loaded = loader.load(law_version=1, turn=TURN, conn=conn)
            db.apply_module_migrations(conn, loaded)
            label = "'{'  (unreadable)" if broken else '\'{"pitched": true}\''
            print(f"\ntent state_json = {label}")
            try:
                report = reports.build(conn, loaded, "p1", TURN)
                notices = [n["type"] for n in report.get("notices", [])]
                print(f"  reports.build   : ok")
                print(f"  notices         : {notices}")
            except BaseException as exc:
                print(f"  reports.build   : {type(exc).__name__}: {exc}")
                print(f"  notices         : the citizen receives no report at all")
        finally:
            conn.close()
            tmp.cleanup()


if __name__ == "__main__":
    sys.path.insert(0, str(Path(__file__).resolve().parent))
    main()
