"""Review rules: stars-then-text (U-25) and who a reply is shown as (N-10, REV-049)."""
import sys, datetime as dt
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import rules  # noqa: E402

R = {"id": "x", "stars": 4.2, "c": [4, 5, 4, 5, None, 4, 3], "text": "", "published_at": "2026-11-09"}


def test_add_text_later_keeps_stars_and_uses_no_quota():
    new, info = rules.add_text_later(R, "Clear briefings.", dt.date(2026, 11, 20))
    assert new["stars"] == R["stars"] and new["c"] == R["c"]
    assert info == {"new_review": False, "quota_used": 0, "label": "Text added 20 Nov 2026"}


def test_add_text_only_once():
    new, _ = rules.add_text_later(R, "First text.", dt.date(2026, 11, 20))
    with pytest.raises(ValueError):
        rules.add_text_later(new, "Second text.", dt.date(2026, 11, 21))


def test_stars_locked_until_6_months():
    with pytest.raises(PermissionError):
        rules.change_stars(R, [5] * 7, dt.date(2027, 5, 8))
    assert rules.change_stars(R, [5] * 7, dt.date(2027, 5, 9))["replace"] is True


@pytest.mark.parametrize("review,wants,plan,own_thread,expect", [
    ({"anonymous": True}, False, True, True, (True, False)),    # under your own Anonymous review: Anonymous, locked (N-10)
    ({"anonymous": True}, False, False, True, (True, False)),   # ... also after the plan ended (SUG-31)
    ({"anonymous": True}, True, True, False, (True, True)),     # Anonymous review, someone else's thread: you choose
    ({"anonymous": True}, False, True, False, (False, True)),
    ({"anonymous": False}, True, True, False, (False, False)),  # named review: replies named, no toggle
    ({"anonymous": False}, True, True, True, (False, False)),
    (None, True, True, False, (True, True)),                    # no review of this school: per-reply choice (REV-049)
    (None, True, False, False, (False, False)),                 # Free: never anonymous, no toggle
])
def test_reply_identity(review, wants, plan, own_thread, expect):
    assert rules.reply_identity(review, wants, plan, on_own_thread=own_thread) == expect
