"""Export a reflexivity proof, check its article, and publish it for reuse."""

from pathlib import Path
from tempfile import TemporaryDirectory

from judgment import Article, Budget, Library, Session


def main():
    with TemporaryDirectory() as directory:
        path = str(Path(directory) / "reflexivity.art")
        budget = Budget(10_000_000)
        s = Session()
        proof = s.theorem("truth_refl", s.true_() == s.true_())
        proof.rfl()
        theorem = proof.finish()
        exports = [s.export(theorem, "basics.truth_refl")]
        Article.write(path, exports, budget)  # verify=True: checked read-back before publication.

        article = Article.open(path)
        entries = article.theorems()
        assert len(entries) == 1
        print("Checked article theorems:", len(entries))
        receiving = Session()
        imported = receiving.import_theorem(article, entries[0].name)
        assert imported.imported() is not None
        print("Imported statement:", imported.statement().format())
        print("Recorded as an import:", imported.imported() is not None)

        # Keep the checked article in a binary library for future processes.
        library_path = str(Path(directory) / "library")
        library = Library.create(library_path, "none")
        change = library.change()
        change.add_article(path)
        change.publish()
        reopened = Library.open(library_path)
        checked = Session.open(reopened.snapshot(budget))
        recovered = checked.import_fact(reopened.theorems()[0].name)
        assert len(recovered.axioms()) == 0
        assert len(recovered.admissions()) == 0
        print("From checked binary snapshot:", recovered)


if __name__ == "__main__":
    main()
