"""Paid school extras (DESIGN PENDING, PREM-1). Real builds never carry them; the preview flag draws SAMPLE posts only for a
paid or gifted school, and keeps them [hidden] until ?preview-premium=1."""
import json, sys, datetime as dt
from pathlib import Path

HERE = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(HERE))
import generator  # noqa: E402

TODAY = dt.date(2026, 11, 19)
PAID = json.loads((HERE / "data/ato/example-flight-academy.json").read_text())
UNPAID = json.loads((HERE / "data/ato/example-air-school.json").read_text())
LAPSED = json.loads((HERE / "data/ato/example-lapsed-school.json").read_text())


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


def test_real_builds_have_no_extras(tmp_path):
    html = render(tmp_path, PAID)
    assert "pr-news" not in html and "pr-zone" not in html and "data-design-pending" not in html


def test_preview_only_on_paid_schools(tmp_path):
    for ato in (UNPAID, LAPSED):
        assert "pr-zone" not in render(tmp_path / ato["slug"], ato, premium_preview=True)


def test_preview_blocks_start_hidden(tmp_path):
    html = render(tmp_path, PAID, premium_preview=True)
    assert html.count('data-design-pending="premium-extras" hidden') == 2
    # The announcement leads, straight under the hero; the school's own posts sit after the SACAA courses.
    assert html.index("pr-news") < html.index("ob-about-sec") < html.index("ob-crs") < html.index("pr-zone") < html.index("ob-rateblock")


def test_ledger_joins_fee_and_intake_and_dates_them(tmp_path):
    html = render(tmp_path, PAID, premium_preview=True)
    assert "R165,000" in html and "12 Jan 2027" in html and "6 places" in html
    assert "Updated 12 Nov 2026" in html
    m = generator.premium_model({"price": [], "intake": [{"title": "Night rating", "date": "2027-01-20"}]})
    assert m["ledger"] == [{"title": "Night rating", "includes": "", "fee": None, "intake": "20 Jan 2027", "seats": None}]


def test_featured_fleet_comes_first():
    m = generator.premium_model({"fleet": [{"title": "PA-28", "count": 2}, {"title": "C172", "count": 4, "featured": True}]})
    assert [f["title"] for f in m["fleet"]] == ["C172", "PA-28"]
