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

from std.tempfile import mkdtemp
from std.testing import assert_equal, assert_true
from judgment import Session
from judgment_store import Article, Library
from termbank import Budget


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

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

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