#!/usr/bin/env python3
"""shadow_signatures.py — a STABLE, deterministic signature of what the digest engine sees
in a vault. For migration regression testing: run it against two checkouts (e.g. the
current vault and an Alexandria-rendered copy) and diff the output. Byte-identical output
means the engine behaves identically — the safety gate for the Phase-3 vault migration.

No Claude, no email — pure read. Prints JSON with: per owner_digest, the deterministic
dry-run payload (digest_payload); the rendered Action Board; and a sorted list of item
signatures (area | due | check | tier | text) across every todo. Run:

    python -m alexandria_vault.engine.shadow_signatures --vault /path/to/vault
"""
import json
import sys

from alexandria_vault.engine import consume_vault_arg

# `--vault` must land in VAULT_DIR before digest freezes its locator at import.
consume_vault_arg()

from alexandria_vault.engine import digest  # noqa: E402


def item_signatures():
    """Sorted, stable one-line signature per collected todo — independent of ordering, so a
    reordering doesn't show as a diff but any content/date/tier/area change does."""
    sigs = []
    for it in digest.collect_todos():
        sigs.append(" | ".join([
            str(it.get("area", "")),
            str(it.get("due") or ""),
            str(it.get("check") or ""),
            str(it.get("tier") if it.get("tier") is not None else ""),
            digest.clean(it.get("text", "")),
        ]))
    return sorted(sigs)


def main():
    out = {"digests": {}, "item_signatures": item_signatures()}
    for spec in digest.digest_specs():
        payload = digest.build_payload(area_filter=spec["area_filter"],
                                       personal=spec["personal"], scope=spec["scope"])
        out["digests"][spec["key"]] = digest.digest_payload(payload)
    out["action_board"] = digest.render_action_board(digest.build_payload())
    print(json.dumps(out, indent=2, ensure_ascii=False, sort_keys=True))


if __name__ == "__main__":
    main()
