"""Does a citizen's turn report tell them the number of points they will have?

`actions.ap_budget_of` is the world's answer: the *smaller* of the standing
budget and the newcomer allowance, for the batches inside the landing window.
`reports.build` reads `players.ap_budget` — the standing budget alone — and
prints it under "N action points next turn".

    cd research/hesper/pr4-review && python report_ap_header.py

Found while reviewing PR #4, which corrected the same class of claim in the
upkeep notice. This one is older and is not the PR's.
"""
import sys
from pathlib import Path

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

from hesper import actions, db, reports
from hesper.core import loader
from broken_tent_turn import world

TURN = 3          # the batch this report points at is TURN + 1


def main():
    tmp, conn = world(broken=False)
    try:
        loaded = loader.load(law_version=1, turn=TURN, conn=conn)
        db.apply_module_migrations(conn, loaded)
        consts = loaded.constants if hasattr(loaded, "constants") else None
        if consts is None:                     # the loader's shape varies by call
            consts = {"ap_newcomer": 5, "ap_newcomer_turns": 4}
        for landed, label in ((TURN - 1, "a newcomer, landed last turn"),
                              (None, "settled: landed long ago")):
            conn.execute("UPDATE players SET landed_turn = ?, ap_budget = 10 "
                         "WHERE player_id = 'p1'", (landed if landed is not None else 0,))
            if landed is None:
                conn.execute("UPDATE players SET landed_turn = -99 WHERE player_id = 'p1'")
            player = conn.execute("SELECT * FROM players WHERE player_id='p1'").fetchone()
            truth = actions.ap_budget_of(player, TURN + 1, consts)
            report = reports.build(conn, loaded, "p1", TURN)
            said = report["ap_budget"]
            mark = "" if said == truth else "   <-- the report is wrong"
            print(f"  {label:<32} report says {said}, "
                  f"ap_budget_of says {truth}{mark}")
    finally:
        conn.close()
        tmp.cleanup()


if __name__ == "__main__":
    print("standing budget 10 in both rows; only the landing window differs\n")
    main()
