"""same_title.py - what happens when the same task is created twice?

    python same_title.py

The agent here has no program, so its tasks stay open for as long as we like.
"""
import scratch


def create(made, agent, title):
    return scratch.send("POST", "/companies/" + made["id"] + "/issues",
                        {"title": title, "description": "2 water", "status": "todo",
                         "assigneeAgentId": agent["id"] if agent else None})


def same(one, two):
    return "the same task came back" if one["id"] == two["id"] else "a new task was made"


def main():
    made, boss = scratch.company("Check same title")
    holder = scratch.idle_agent(made, boss, "Holder")
    other = scratch.idle_agent(made, boss, "Other")

    first = create(made, holder, "Bill: 2 water")
    again = create(made, holder, "Bill: 2 water")
    print("Same title, same agent, the first still open:", same(first, again))
    elsewhere = create(made, other, "Bill: 2 water")
    print("Same title, another agent:", same(first, elsewhere))
    nobody = create(made, None, "Bill: 2 water")
    print("Same title, given to nobody:", same(first, nobody))
    different = create(made, holder, "Bill: 3 water")
    print("Another title, same agent:", same(first, different))
    scratch.send("PATCH", "/issues/" + first["id"], {"status": "cancelled"})
    after = create(made, holder, "Bill: 2 water")
    print("Same title, same agent, after the first was cancelled:", same(first, after))
    scratch.remove(made)


if __name__ == "__main__":
    main()
