"""Empty-field lines and the claim invite (t1536u)."""
import copy, json, datetime as dt
from pathlib import Path
import pytest

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())


def _gap_school(**over):
    a = {
        "id": "SACAA/0001/ATO",
        "slug": "gap-school",
        "name": "Gap School",
        "bases": [{"ident": None, "name": "", "town": "", "href": None,
                   "certificate": {"number": "SACAA/0001/ATO", "valid_until": None}}],
        "about": "",
        "contact": {"phone": None, "email": None, "web": None},
        "courses": [],
        "top_reviews": [],
    }
    a.update(over)
    generator.check(a)
    return a


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 test_empty_fields_show_muted_lines(tmp_path):
    html = _html(tmp_path, _gap_school())
    assert "This school hasn't added a description yet." in html
    assert "No contact details yet." in html
    assert "Base aerodrome not listed." in html
    assert "ap-adl" not in html                    # leave out the aerodrome card
    assert "Courses not listed on the SACAA record." in html
    assert "Expiry not listed on the SACAA record." not in html
    assert ">Unknown<" in html
    assert 'class="ob-st">Unknown</em>' in html
    assert 'class="ob-gap"' in html                 # other empty fields still use the muted gap line


def test_example_pages_keep_real_copy(tmp_path):
    html = _html(tmp_path, copy.deepcopy(PAID))
    assert "This school hasn't added a description yet." not in html
    assert "No contact details yet." not in html
    assert "Base aerodrome not listed." not in html
    assert "Courses not listed on the SACAA record." not in html
    assert "Expiry not listed on the SACAA record." not in html
    assert 'class="ob-claim-invite"' not in html   # claimed (paid)


def test_claim_invite_on_unclaimed_gaps_only(tmp_path):
    html = _html(tmp_path, _gap_school())
    assert 'class="ob-claim-invite"' in html
    assert "Is this your school? Add your info — claim this page" in html
    assert 'href="/ato/claim?slug=gap-school"' in html


def test_claim_invite_hidden_once_claimed(tmp_path):
    a = _gap_school()
    a["status"] = "verified"
    html = _html(tmp_path, a)
    assert "This school hasn't added a description yet." in html   # gaps still shown
    assert 'class="ob-claim-invite"' not in html


def test_claim_invite_hidden_when_nothing_is_missing(tmp_path):
    a = copy.deepcopy(PAID)
    a["status"] = "unclaimed"   # not claimed, but the page is complete
    html = _html(tmp_path, a)
    assert 'class="ob-claim-invite"' not in html


def test_unknown_expiry_uses_existing_pill_not_gap(tmp_path):
    """A certificate with no expiry date shows the word Unknown in the existing status pill."""
    html = _html(tmp_path, _gap_school())
    assert "Expiry not listed on the SACAA record." not in html
    assert '<em class="ob-st">Unknown</em>' in html
    assert "ob-cur" not in html.split("Unknown", 1)[0][-80:]  # the Unknown pill is not Current
    # The claim invite still treats a missing expiry as a gap on an unclaimed page.
    assert 'class="ob-claim-invite"' in html


def test_dated_certificate_is_current_or_expired_not_unknown(tmp_path):
    html = _html(tmp_path, copy.deepcopy(PAID))
    assert ">Current<" in html and ">Expired<" in html
    assert '<em class="ob-st">Unknown</em>' not in html
    assert "Expiry not listed on the SACAA record." not in html
