"""Save a checked theorem and recover it in a fresh session."""

from pathlib import Path
from tempfile import TemporaryDirectory

from judgment import Budget, Identity, Library, Session


def main():
    # Use your own persistent directory to keep the library between runs.
    with TemporaryDirectory() as directory:
        path = str(Path(directory) / "my_library")
        budget = Budget(10_000_000)
        library = Library.create(path, "none")
        s = Session.open(library.profile_snapshot(budget))
        proof = s.theorem("truth_refl", s.true_() == s.true_())
        proof.rfl()
        theorem = proof.finish()

        change = library.change()
        change.add(s.export(theorem, "basics.truth_refl"))
        version = change.publish()
        print("Published theorems:", len(Library.open(path).theorems()))

        # This part can run in another process, with only the directory and version.
        saved_version = str(version)
        reopened = Library.open(path, version=Identity.from_hex(saved_version))
        loaded = Session.open(reopened.snapshot(budget))
        recovered = loaded.import_fact("basics.truth_refl")
        assert reopened.version() == version
        assert len(recovered.axioms()) == 0
        assert len(recovered.admissions()) == 0
        print("Reopened:", recovered)
        reuse = loaded.theorem("reused", recovered.statement())
        reuse.exact(recovered)
        print("Reused:", reuse.finish())


if __name__ == "__main__":
    main()
