"""Editing a comment, and what it used to say.

Anyone can make a mistake, so anyone can fix their own. But an edit that
silently rewrites the record is the wrong thing in a client review — "you said
X" / "no, I said Y" is precisely the dispute this tool exists to prevent.
"""

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


def _pin_comment(client, oid, body="First wording."):
    pin = client.post(f"/api/options/{oid}/pins", json={
        "x_percent": 5.0, "y_percent": 5.0, "body_md": body}).json()["pin"]
    return pin["comments"][0]["id"]


def test_the_author_can_fix_their_own_mistake(client, kit):
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    cid = _pin_comment(client, oid, "Can the phone nubmer be bigger?")

    r = client.patch(f"/api/comments/{cid}",
                     json={"body_md": "Can the phone number be bigger?"})
    assert r.status_code == 200
    c = client.get(f"/api/options/{oid}/pins").json()["pins"][0]["comments"][0]
    assert c["body_md"] == "Can the phone number be bigger?"
    assert c["edited_at"] is not None


def test_the_earlier_wording_survives(client, kit):
    """The point of the whole feature: an edit may correct a mistake, it may not
    make the earlier version disappear."""
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    cid = _pin_comment(client, oid, "Make the hero smaller.")
    client.patch(f"/api/comments/{cid}", json={"body_md": "Make the hero bigger."})
    client.patch(f"/api/comments/{cid}", json={"body_md": "Leave the hero alone."})

    history = client.get(f"/api/comments/{cid}/history").json()["revisions"]
    # Oldest first: the original wording is the first entry.
    assert [r["body_md"] for r in history] == [
        "Make the hero smaller.", "Make the hero bigger."]
    assert all(r["replaced_at"] for r in history)


def test_everyone_who_can_read_the_comment_can_read_its_history(client, kit):
    """A history only the author can see is not a record, it is a diary."""
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    cid = _pin_comment(client, oid, "Original.")
    client.patch(f"/api/comments/{cid}", json={"body_md": "Changed."})

    as_user(client, "mara")
    assert client.get(f"/api/comments/{cid}/history").json()["revisions"][0][
        "body_md"] == "Original."
    as_user(client, "nate")
    assert client.get(f"/api/comments/{cid}/history").status_code == 404


def test_a_no_op_edit_files_nothing(client, kit):
    """Pressing save without changing a word should not manufacture history."""
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    cid = _pin_comment(client, oid, "Unchanged.")
    client.patch(f"/api/comments/{cid}", json={"body_md": "Unchanged."})
    assert client.get(f"/api/comments/{cid}/history").json()["revisions"] == []
    assert client.get(f"/api/options/{oid}/pins").json()["pins"][0][
        "comments"][0]["edited_at"] is None


def test_a_manager_may_remove_a_note_but_never_rewrite_it(client, kit):
    """Removing someone's note is moderation; rewriting it is putting words in
    their mouth. The API draws that line and the UI does not soften it."""
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    cid = _pin_comment(client, oid, "The client's own words.")

    as_user(client, "mara")
    assert client.patch(f"/api/comments/{cid}",
                        json={"body_md": "Something else"}).status_code == 403
    assert client.delete(f"/api/comments/{cid}").status_code == 200


def test_history_survives_the_comment_being_removed(client, kit):
    """Soft delete keeps the row; the record of what it said stays readable."""
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    cid = _pin_comment(client, oid, "First.")
    client.patch(f"/api/comments/{cid}", json={"body_md": "Second."})
    client.delete(f"/api/comments/{cid}")
    assert client.get(f"/api/comments/{cid}/history").json()["revisions"][0][
        "body_md"] == "First."
