# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (c) 2026 sol pbc

"""Regression tests for locked U4 pair-flow copy constants."""

from __future__ import annotations

import re

from solstone.apps.network import copy

U4_COPY_NAMES = (
    "MODAL_TITLE",
    "STEP_1",
    "STEP_2",
    "STEP_3",
    "PAIR_NETWORK_LINE",
    "DETAILS_DISCLOSURE",
    "CA_FP_LABEL",
    "CA_FP_NOTE",
    "DEVICE_LABEL_FIELD_LABEL",
    "EXPIRED_BUTTON",
    "PAIR_ERROR_BODY",
    "SUCCESS_HEADING",
    "SUCCESS_SUBHEAD",
    "SUCCESS_DONE",
    "SUCCESS_VERIFY_NOTE",
    "SUCCESS_REMOVE_LABEL",
    "DEVICE_LABEL_PLACEHOLDER",
    "HERO_TITLE",
    "HERO_BODY",
    "HERO_HOW_REACH_LABEL",
)
U4_COPY_VALUES = [getattr(copy, name) for name in U4_COPY_NAMES]

U8_COPY_NAMES = (
    "WINDOW_CLOSED_BUTTON",
    "SUCCESS_VERIFY_NOTE_ANYWHERE",
    "RECENT_NETWORK_LABEL_ANYWHERE",
)
U8_COPY_VALUES = [getattr(copy, name) for name in U8_COPY_NAMES]

PRESENTATION_COPY_NAMES = (
    "PRESENTATION_SELECTOR_LABEL",
    "PRESENTATION_PHONE_LABEL",
    "PRESENTATION_COMPUTER_LABEL",
    "PRESENTATION_GLASSES_LABEL",
    "PAIR_LINK_FIELD_LABEL",
    "PAIR_LINK_COPY_LABEL",
    "PAIR_LINK_COPY_SUCCESS_TOAST",
    "PAIR_LINK_COPY_FAIL_TOAST",
    "REACH_HOME_CANDIDATES_LABEL",
    "REACH_HOME_CANDIDATES_REFRESH_FAIL",
    "REACH_HOME_CANDIDATES_UNAVAILABLE",
    "HOME_CANDIDATES_ERROR",
)
PRESENTATION_COPY_VALUES = [getattr(copy, name) for name in PRESENTATION_COPY_NAMES]


def _link_copy(env) -> dict[str, object]:
    response = env.client.get("/app/network/api/state")
    assert response.status_code == 200
    return response.get_json()["link_copy"]


def test_u4_copy_values_are_locked() -> None:
    assert copy.MODAL_TITLE == "pair a device"
    assert copy.STEP_1 == "open the camera on the device you're adding"
    assert copy.STEP_2 == "point it at this code"
    assert copy.STEP_3 == "tap the link to open solstone"
    assert copy.PAIR_NETWORK_LINE == (
        "this device needs to be on your network (or your VPN) to pair. "
        "expires in 5:00."
    )
    assert copy.DETAILS_DISCLOSURE == "verify this is really your home"
    assert copy.CA_FP_LABEL == "fingerprint"
    assert copy.CA_FP_NOTE == (
        "the device checks this when it scans, so no one on your wifi can "
        "impersonate home."
    )
    assert copy.DEVICE_LABEL_FIELD_LABEL == "name this device"
    assert copy.DEVICE_LABEL_DEFAULT_FORMAT == "device — added {month} {day}"
    assert copy.EXPIRED_BUTTON == "this code expired — show a new one"
    assert copy.PAIR_ERROR_BODY == (
        "can't start pairing — your journal isn't reachable on a network address yet."
    )
    assert copy.SUCCESS_HEADING == '"{label}" is now paired with your journal'
    assert copy.SUCCESS_SUBHEAD == "{short_fp} · paired just now"
    assert copy.SUCCESS_DONE == "done"
    assert copy.SUCCESS_VERIFY_NOTE == (
        "check the device you just paired — this fingerprint should match what it "
        "shows. didn't do this?"
    )
    assert copy.SUCCESS_REMOVE_LABEL == "that wasn't me — remove"
    assert copy.DEVICE_LABEL_PLACEHOLDER == "e.g. my iPhone"
    assert copy.HERO_TITLE == "let's connect a device"
    assert copy.HERO_BODY == (
        "your journal lives here, on this device. to read it from your phone or "
        "laptop, that device needs a way to reach it. right now it can be reached "
        "on your home network."
    )
    assert copy.HERO_HOW_REACH_LABEL == "how reach works ▸"


def test_u8_copy_values_are_locked() -> None:
    assert copy.WINDOW_CLOSED_BUTTON == "pairing window closed — open a new one"
    assert copy.SUCCESS_VERIFY_NOTE_ANYWHERE == (
        "this device can now reach home from anywhere. check it now — "
        "this fingerprint should match what it shows. didn't do this?"
    )
    assert copy.RECENT_NETWORK_LABEL_ANYWHERE == "from anywhere"


def test_presentation_and_home_candidate_copy_values_are_locked() -> None:
    assert copy.PRESENTATION_SELECTOR_LABEL == "pairing display"
    assert copy.PRESENTATION_PHONE_LABEL == "phone"
    assert copy.PRESENTATION_COMPUTER_LABEL == "computer"
    assert copy.PRESENTATION_GLASSES_LABEL == "glasses"
    assert copy.PAIR_LINK_FIELD_LABEL == "pairing link"
    assert copy.PAIR_LINK_COPY_LABEL == "copy link"
    assert copy.PAIR_LINK_COPY_SUCCESS_TOAST == "pairing link copied"
    assert copy.PAIR_LINK_COPY_FAIL_TOAST == "couldn't copy pairing link"
    assert copy.REACH_HOME_CANDIDATES_LABEL == "choose a home address"
    assert (
        copy.REACH_HOME_CANDIDATES_REFRESH_FAIL
        == "saved, but couldn't refresh. showing the last confirmed address."
    )
    assert (
        copy.REACH_HOME_CANDIDATES_UNAVAILABLE
        == "couldn't check home addresses. you can still type one below."
    )
    assert copy.HOME_CANDIDATES_ERROR == "couldn't check home addresses"

    for value in PRESENTATION_COPY_VALUES:
        assert value == value.lower()


def test_u4_copy_stays_in_bounds() -> None:
    banned_terms = ("account",)
    acronym_re = re.compile(r"\b(dl|pl|spl)\b")
    device_noun_re = re.compile(r"\bphone\b")

    for value in U4_COPY_VALUES:
        lowered = value.lower()
        for term in banned_terms:
            assert term not in lowered, value
        assert not acronym_re.search(lowered), value

    for value in U4_COPY_VALUES:
        if value == copy.HERO_BODY:
            continue
        assert not device_noun_re.search(value.lower()), value

    assert "phone or laptop" in copy.HERO_BODY.lower()


def test_u8_copy_stays_in_bounds() -> None:
    banned_terms = ("account",)
    acronym_re = re.compile(r"\b(dl|pl|spl)\b")
    device_noun_re = re.compile(r"\bphone\b")

    for value in U8_COPY_VALUES:
        lowered = value.lower()
        for term in banned_terms:
            assert term not in lowered, value
        assert not acronym_re.search(lowered), value
        assert not device_noun_re.search(lowered), value


def test_u4_copy_matches_state_payload(link_env) -> None:
    env = link_env()
    payload = _link_copy(env)

    for name in U4_COPY_NAMES:
        assert payload[name] == getattr(copy, name)


def test_presentation_copy_matches_state_payload(link_env) -> None:
    env = link_env()
    payload = _link_copy(env)

    for name in PRESENTATION_COPY_NAMES:
        assert payload[name] == getattr(copy, name)
