"""Lapsed school state: keeps .official and the claim, loses the verified badge and posting."""
import copy, json, re, datetime as dt
from pathlib import Path

import generator

TODAY = dt.date(2026, 11, 11)
HERE = Path(__file__).resolve().parents[1]
PAID = json.loads((HERE / "data/ato/example-flight-academy.json").read_text())
LAPSED = json.loads((HERE / "data/ato/example-lapsed-school.json").read_text())
GOLD = "#F5B83D"


def _html(tmp_path, ato):
    d = tmp_path / "data"
    d.mkdir()
    (d / "a.json").write_text(json.dumps(ato))
    generator.build(d, tmp_path / "out", "amethyst", TODAY)
    return (tmp_path / "out" / ato["slug"] / "index.html").read_text()


def _name_line(html):
    return re.search(r'<h1 class="ob-name">.*?</h1>', html, re.S).group(0)


def _hero(html):
    return html.split('<section class="ob-card ob-hero">', 1)[1].split("</section>", 1)[0]


def test_lapsed_status_is_accepted():
    a = copy.deepcopy(LAPSED)
    assert generator.check(a) == []
    m = generator.page_model(a, TODAY)
    assert m["paid"] is False
    assert m["lapsed"] is True
    assert m["verified"] is True          # claim and .official stay
    assert m["can_reply"] is False        # posting and replying are off
    assert m["show_claim_invite"] is False


def test_lapsed_keeps_official_handle_and_loses_badge(tmp_path):
    html = _html(tmp_path, copy.deepcopy(LAPSED))
    hero = _hero(html)
    assert ".official" in hero
    assert "examplelapsed" in hero
    assert "Not claimed by the school yet" not in hero
    assert "ico-rosette" not in _name_line(html)
    assert GOLD not in _name_line(html)
    assert 'class="ob-claim-invite"' not in html
    assert 'data-status="lapsed"' in html
    assert 'data-paid="false"' in html
    assert 'data-lapsed="true"' in html
    assert 'data-can-reply="false"' in html


def test_lapsed_keeps_existing_official_replies(tmp_path):
    html = _html(tmp_path, copy.deepcopy(LAPSED))
    assert "Example reply from when this school was a paying official school." in html
    assert "Example reply from another verified ATO. This stays on the page after the school lapses." not in html  # own-school official replies only
    own = re.search(r'<div class="ob-reply is-own"><strong>(.*?)</strong>', html, re.S)
    assert own and "examplelapsed" in own.group(1) and ".official" in own.group(1)
    assert GOLD not in own.group(1)                 # own-reply tick follows the current (not paying) state
    assert re.search(r'<div class="ob-reply is-other">', html) is None


def test_paid_school_still_has_badge_and_can_reply(tmp_path):
    html = _html(tmp_path, copy.deepcopy(PAID))
    assert "ico-rosette" in _name_line(html) and GOLD in _name_line(html)
    assert 'data-status="paid"' in html
    assert 'data-paid="true"' in html
    assert 'data-lapsed="false"' in html
    assert 'data-can-reply="true"' in html
    assert 'class="ob-claim-invite"' not in html


def test_paid_school_becomes_lapsed(tmp_path):
    a = copy.deepcopy(PAID)
    a["status"] = "lapsed"
    m = generator.page_model(copy.deepcopy(a), TODAY)
    assert m["paid"] is False and m["lapsed"] is True and m["verified"] is True and m["can_reply"] is False
    html = _html(tmp_path, a)
    assert ".official" in _hero(html)
    assert "ico-rosette" not in _name_line(html)
    # Existing own official reply from the paid fixture stays on the page.
    assert "We added a second trainer to the summer roster from November." in html
