"""Tests for GIAS establishment status exposure.

"Open, but proposed to close" schools are now kept by the dims; the API must
surface `status` on list items and school_info so the UI can render the
proposed-to-close marker (listing tag) and notice strip (detail page).
"""

import numpy as np
import pandas as pd
import pytest
from fastapi.testclient import TestClient

PROPOSED = "Open, but proposed to close"


def _schools_df() -> pd.DataFrame:
    base = {
        "local_authority": "Testshire",
        "school_type": "Academy",
        "phase": "Secondary",
        "address": "1 Test Street",
        "town": "Testtown",
        "postcode": "TS1 1AA",
        "religious_denomination": None,
        "gender": "Mixed",
        "age_range": "11-16",
        "admissions_policy": None,
        "has_sixth_form": False,
        "ofsted_grade": np.nan,
        "ofsted_date": None,
        "ofsted_framework": None,
        "latitude": 51.5,
        "longitude": -0.1,
        "year": 202425,
        "total_pupils": 800,
        "rwm_expected_pct": np.nan,
        "attainment_8_score": 48.0,
    }
    return pd.DataFrame(
        [
            {**base, "urn": 200001, "school_name": "Alpha Academy",
             "status": "Open"},
            {**base, "urn": 200002, "school_name": "Sarson High School",
             "status": PROPOSED},
        ]
    )


@pytest.fixture()
def client(monkeypatch):
    from backend import app as app_module

    monkeypatch.setattr(app_module, "load_latest_school_data", _schools_df)
    monkeypatch.setattr(app_module, "load_school_data", _schools_df)
    monkeypatch.setattr(app_module, "get_supplementary_data", lambda db, urn: {})
    return TestClient(app_module.app, raise_server_exceptions=False)


def test_list_payload_includes_status(client):
    resp = client.get("/api/schools")
    assert resp.status_code == 200, resp.text
    by_urn = {s["urn"]: s for s in resp.json()["schools"]}
    assert by_urn[200001]["status"] == "Open"
    assert by_urn[200002]["status"] == PROPOSED


def test_detail_payload_includes_status(client):
    resp = client.get("/api/schools/200002")
    assert resp.status_code == 200, resp.text
    assert resp.json()["school_info"]["status"] == PROPOSED
