"""Every comment from the client side reaches the team.

Mentions and replies in a thread you are in cover a conversation already
going. They say nothing when a client starts one, which is most client
feedback: a client left forty notes on a design and nobody on the team was
told. So each client comment tells the team, once each.

Cast (tests/test_domain.py): rian owner · mara admin (manager side) · cleo
client on the project · nate a member elsewhere.
"""

from tests.conftest import as_user
from tests.test_domain import add_screen_option, make_project


def _bell(client, user):
    as_user(client, user)
    return client.get("/api/notifications").json()["notifications"]


def _note(client, oid, body="The logo feels small"):
    r = client.post(f"/api/options/{oid}/pins",
                    json={"x_percent": 5, "y_percent": 5, "body_md": body})
    assert r.status_code == 200, r.text
    return r.json()["pin"]


def test_a_new_client_note_reaches_the_owner_and_the_team(client, kit):
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    _note(client, oid)
    for user in ("rian", "mara"):
        kinds = [(n["kind"], n["actor"]) for n in _bell(client, user)]
        assert ("comment", "cleo") in kinds, (user, kinds)
    assert _bell(client, "cleo") == []      # never the author
    assert _bell(client, "nate") == []      # never outside the project


def test_a_team_note_does_not_ping_the_client(client, kit):
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "mara")
    _note(client, oid, "Internal: check the spacing here")
    assert _bell(client, "cleo") == []
    assert _bell(client, "rian") == []      # the team's own notes are not news


def test_a_client_reply_reaches_a_team_not_yet_in_the_thread(client, kit):
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    pin = _note(client, oid)
    r = client.post(f"/api/threads/{pin['thread_id']}/comments",
                    json={"body_md": "And the tagline under it"})
    assert r.status_code == 200, r.text
    assert [n["kind"] for n in _bell(client, "mara")] == ["comment", "comment"]


def test_nobody_is_told_twice_about_one_comment(client, kit):
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    pin = _note(client, oid)
    as_user(client, "mara")
    client.post(f"/api/threads/{pin['thread_id']}/comments", json={"body_md": "On it"})
    as_user(client, "cleo")
    client.post(f"/api/threads/{pin['thread_id']}/comments",
                json={"body_md": "@mara thanks, and one more thing"})
    # mara: a comment for the first note, then ONE notification for the last
    # comment (the mention), not a mention and a reply and a comment.
    last = [n for n in _bell(client, "mara") if n["kind"] != "comment"]
    assert [n["kind"] for n in last] == ["mention"]
    # rian was in none of it: told once per client comment, as a comment.
    assert [n["kind"] for n in _bell(client, "rian")] == ["comment", "comment"]
