"""Create a proof-DAG library, add theorems from articles, refuse a withdrawal that something depends on, and publish
a change again after a conflict."""
from pathlib import Path
from tempfile import TemporaryDirectory

from judgment import Library, StoreError

# An OpenTheory article: ⊢ p = p for a variable p : bool, by reflexivity.
TYPES = (
    "6\nversion\n"
    '"bool"\ntypeOp\nnil\nopType\n0\ndef\npop\n'
    '"->"\ntypeOp\n0\nref\n0\nref\nnil\ncons\ncons\nopType\n1\ndef\npop\n'
    '"->"\ntypeOp\n0\nref\n1\nref\nnil\ncons\ncons\nopType\n2\ndef\npop\n'
    '"p"\n0\nref\nvar\n3\ndef\npop\n3\nref\nvarTerm\n4\ndef\npop\n'
    '"="\nconst\n2\nref\nconstTerm\n4\nref\nappTerm\n4\nref\nappTerm\n5\ndef\npop\n'
)
REFL = TYPES + "4\nref\nrefl\nnil\n5\nref\nthm\n"
# ⊢ (p = p) = (p = p), from ⊢ p = p cited as an axiom: the library's theorem.
TWICE = (
    TYPES
    + "nil\n5\nref\naxiom\n6\ndef\npop\n6\nref\n6\nref\ndeductAntisym\n"
    + '"="\nconst\n2\nref\nconstTerm\n5\nref\nappTerm\n5\nref\nappTerm\n7\ndef\npop\nnil\n7\nref\nthm\n'
)


def article(directory: Path, name: str, text: str) -> str:
    path = directory / f"{name}.art"
    path.write_text(text)
    return str(path)


def theorems(lib: Library) -> int:
    return sum(1 for t in lib.theorems() if t.kind == "theorem")


def main():
    with TemporaryDirectory() as tmp:
        directory = Path(tmp)
        path = str(directory / "lib")

        # A new library: its first version holds the profile's statements and nothing else.
        lib = Library.create(path, "none")
        print("Created; theorems:", theorems(lib))

        # Each change is published as the next version, atomically.
        change = lib.change()
        change.add_article(article(directory, "refl", REFL))
        previous = change.publish()
        change = Library.open(path).change()
        change.add_article(article(directory, "twice", TWICE))
        change.publish()
        lib = Library.open(path)
        print("After two changes; theorems:", theorems(lib), "| base is the previous version:", change.base() == previous)

        # p = p is cited by the second theorem's proof: withdrawing it is refused. Nothing cites the second theorem.
        for t in lib.theorems():
            if t.kind != "theorem":
                continue
            withdraw = lib.change()
            try:
                withdraw.withdraw(str(t.sid))
                print("Withdrawing a theorem nothing cites: accepted")
            except StoreError as e:
                print("Withdrawing a cited theorem:", e.kind)

        # Two writers open the same version. The second publishes after the first: it is refused, rebased onto the new
        # version, and published there.
        first = Library.open(path).change()
        second = Library.open(path).change()
        first.add_article(article(directory, "first", REFL))
        second.add_article(article(directory, "second", REFL))
        by_first = first.publish()
        try:
            second.publish()
        except StoreError as e:
            print("The second writer:", e.kind)
        second.rebase()
        second.publish()
        print("Published over the first writer's version:", second.base() == by_first)


if __name__ == "__main__":
    main()
