"""A note remembers which variation it was left on.

A pin is a point on a DESIGN, and a toggle changes the design — "make this
bigger" against the hexagon header is not the same remark as against the plain
one. Without this the two remarks are indistinguishable a week later.
"""

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


def _brand_axis(client, oid):
    for key, label, css, default in [
        ("quiet", "Hexagon light", "v-brand-quiet", True),
        ("hive", "Full hive", "v-brand-hive", False),
    ]:
        client.post(f"/api/options/{oid}/variants", json={
            "axis": "brand", "axis_label": "Brand", "key": key,
            "label": label, "css_class": css, "is_default": default})


def test_a_note_records_the_variation_it_was_left_on(client, kit):
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    _brand_axis(client, oid)

    as_user(client, "cleo")
    r = client.post(f"/api/options/{oid}/pins", json={
        "x_percent": 40.0, "y_percent": 12.0,
        "body_md": "The hexagons feel heavy here.",
        "variants": {"brand": "hive"}})
    assert r.status_code == 200, r.text
    pin = r.json()["pin"]
    assert pin["variants"] == {"brand": "hive"}
    # Reported in the client's own words, not our keys.
    assert pin["variant_labels"] == ["Full hive"]


def test_two_notes_on_the_same_spot_stay_distinguishable(client, kit):
    """The whole point: same coordinates, different design, different remark."""
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    _brand_axis(client, oid)

    as_user(client, "cleo")
    client.post(f"/api/options/{oid}/pins", json={
        "x_percent": 40.0, "y_percent": 12.0, "body_md": "Plain header is clean.",
        "variants": {"brand": "quiet"}})
    client.post(f"/api/options/{oid}/pins", json={
        "x_percent": 40.0, "y_percent": 12.0, "body_md": "Hexagons feel heavy.",
        "variants": {"brand": "hive"}})

    pins = client.get(f"/api/options/{oid}/pins").json()["pins"]
    assert [p["variant_labels"] for p in pins] == [["Hexagon light"], ["Full hive"]]


def test_a_note_on_an_option_without_variations_records_nothing(client, kit):
    """Null, not an empty dict pretending to be a choice."""
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    as_user(client, "cleo")
    pin = client.post(f"/api/options/{oid}/pins", json={
        "x_percent": 5.0, "y_percent": 5.0, "body_md": "A note."}).json()["pin"]
    assert pin["variants"] is None
    assert pin["variant_labels"] == []


def test_notes_from_every_variation_are_returned_together(client, kit):
    """Deliberately NOT filtered to the current variation: a client who flips a
    toggle and finds their own note gone concludes the tool lost it."""
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    _brand_axis(client, oid)
    as_user(client, "cleo")
    client.post(f"/api/options/{oid}/pins", json={
        "x_percent": 10.0, "y_percent": 10.0, "body_md": "One.",
        "variants": {"brand": "quiet"}})
    client.post(f"/api/options/{oid}/pins", json={
        "x_percent": 20.0, "y_percent": 20.0, "body_md": "Two.",
        "variants": {"brand": "hive"}})
    assert len(client.get(f"/api/options/{oid}/pins").json()["pins"]) == 2


def test_the_label_follows_a_renamed_variation(client, kit):
    """Labels are resolved at read time from the option's own variants, so
    renaming a variation does not leave old notes citing a name that is gone."""
    iid = make_project(client, kit)
    _sid, oid = add_screen_option(client, iid)
    _brand_axis(client, oid)
    as_user(client, "cleo")
    client.post(f"/api/options/{oid}/pins", json={
        "x_percent": 5.0, "y_percent": 5.0, "body_md": "A note.",
        "variants": {"brand": "hive"}})

    as_user(client, "mara")
    vid = [v for v in client.get(f"/api/projects/{iid}").json()["screens"][0][
        "options"][0]["variants"] if v["key"] == "hive"][0]["id"]
    client.delete(f"/api/variants/{vid}")
    client.post(f"/api/options/{oid}/variants", json={
        "axis": "brand", "axis_label": "Brand", "key": "hive",
        "label": "Honeycomb", "css_class": "v-brand-hive"})

    pin = client.get(f"/api/options/{oid}/pins").json()["pins"][0]
    assert pin["variant_labels"] == ["Honeycomb"]
