"""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 std.tempfile import mkdtemp
from judgment_store import Library, StoreErrorKind

# An OpenTheory article: ⊢ p = p for a variable p : bool, by reflexivity.
comptime TYPES = String(
    "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",
)
comptime REFL = String(TYPES, "4\nref\nrefl\nnil\n5\nref\nthm\n")
# ⊢ (p = p) = (p = p), from ⊢ p = p cited as an axiom: the library's theorem.
comptime TWICE = String(
    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(dir: String, name: String, text: String) raises -> String:
    var path = String(dir, "/", name, ".art")
    with open(path, "w") as f:
        f.write(text)
    return path


def theorems(lib: Library) -> Int:
    var n = 0
    for t in lib.theorems():
        if t.kind == "theorem":
            n += 1
    return n


def main() raises:
    var dir = mkdtemp()
    var path = dir + "/lib"

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

    # Each change is published as the next version, atomically.
    var change = lib.change()
    change.add_article(article(dir, "refl", REFL))
    var previous = change.publish()
    change = Library.open(path).change()
    change.add_article(article(dir, "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
        var withdraw = lib.change()
        try:
            withdraw.withdraw(String(t.sid))
            print("Withdrawing a theorem nothing cites: accepted")
        except 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.
    var first = Library.open(path).change()
    var second = Library.open(path).change()
    first.add_article(article(dir, "first", REFL))
    second.add_article(article(dir, "second", REFL))
    var by_first = first.publish()
    try:
        _ = second.publish()
    except e:
        print("The second writer:", e.kind)
    second.rebase()
    _ = second.publish()
    print("Published over the first writer's version:", second.base() == by_first)
