"""Audit of 16 Nov 2026: contacts once (hero), no guest bottom-bar padding, default output unchanged by previews."""
import copy, json, re, datetime as dt
from pathlib import Path

import generator

TODAY = dt.date(2026, 11, 16)
HERE = Path(__file__).resolve().parents[1]
PAID = json.loads((HERE / "data/ato/example-flight-academy.json").read_text())
CSS = (HERE / "shared/page.css").read_text()


def render(tmp_path, ato, **kw):
    d = tmp_path / "data"
    d.mkdir(exist_ok=True)
    (d / "a.json").write_text(json.dumps(ato))
    generator.build(d, tmp_path / "out", kw.pop("theme", "midnight"), TODAY, **kw)
    return (tmp_path / "out" / ato["slug"] / "index.html").read_text()


def test_about_has_no_contact_row_and_hero_keeps_all_three(tmp_path):
    html = render(tmp_path, copy.deepcopy(PAID))
    about = html.split("About the ATO", 1)[1].split("</section>", 1)[0]
    assert "ob-clink" not in about and "ob-cbtn" not in about
    hero = html.split('class="ob-card ob-hero"', 1)[1].split("</section>", 1)[0]
    assert hero.count('class="ob-hc"') == 3
    assert 'href="tel:+27000000000"' in hero


def test_missing_contacts_still_disabled_in_hero_and_noted_in_about(tmp_path):
    a = copy.deepcopy(PAID)
    a["contact"] = {"phone": None, "email": None, "web": None}
    html = render(tmp_path, a)
    assert html.count('<span class="ob-hc" aria-disabled="true">') == 3
    assert "No contact details listed yet." in html
    assert "ob-clink" not in html


def test_guest_bottom_padding_dropped_member_keeps_pill_clearance():
    rule = re.search(r'html\[data-preview-view="guest"\] \.ob-root\{padding-bottom:([^}]+)\}', CSS)
    assert rule and "96px" not in rule.group(1) and "env(safe-area-inset-bottom)" in rule.group(1)
    # Only the guest view is targeted; member/owner keep v6.css's 96px clearance for the signed-in nav pill.
    assert 'html[data-preview-view="member"] .ob-root{padding-bottom' not in CSS
    assert not re.search(r"(^|\n)\.ob-root\{[^}]*padding-bottom", CSS)


def test_page_css_cache_bumped(tmp_path):
    html = render(tmp_path, copy.deepcopy(PAID))
    assert "/ato/_shared/page.css?v=20261117h" in html


def test_default_guest_output_has_no_preview_markup(tmp_path):
    html = render(tmp_path, copy.deepcopy(PAID))
    for marker in ("pv-plan", "EXAMPLE DATA · PREVIEW", "data-preview-plan", "ob-form", "Write a review"):
        assert marker not in html


def test_review_fix_16nov_member_empty_state_and_storage_guard(tmp_path):
    """Review 16 Nov (late): members are not told to sign in; theme script survives blocked storage."""
    import copy, json, datetime as dt
    from pathlib import Path
    import generator
    here = Path(__file__).resolve().parents[1]
    a = json.loads((here / "data/ato/example-air-school.json").read_text())
    a["top_reviews"] = []
    d = tmp_path / "d"; d.mkdir()
    (d / "a.json").write_text(json.dumps(a))
    generator.build(d, tmp_path / "o", "midnight", dt.date(2026, 11, 16), viewer={"kind": "member", "plan": "free"})
    html = (tmp_path / "o" / a["slug"] / "index.html").read_text()
    assert "No reviews yet" in html
    assert "Be the first to write one." in html and "Sign in free</a> to write the first one" not in html
    head = html.split("</script>", 1)[0]
    assert "localStorage.getItem(" in head and head.count("localStorage") == 1   # only inside the guarded helper
    js = (here / "shared/topbar.js").read_text()
    assert js.count("localStorage") == 2   # only inside lsGet / lsSet
