"""Supplementary-block enrichment for the compare redesign: report-card
labels, provider-page URL, graded-vs-carried-forward provenance, and the
admissions preference/cross-LA detail promoted in the data-foundation PR."""

import types
from datetime import date

from backend.data_loader import _admissions_row_dict, _ofsted_block


def _row(**kw):
    base = dict(
        framework="RC", inspection_date=None, inspection_type=None,
        overall_effectiveness=None, quality_of_education=None,
        behaviour_attitudes=None, personal_development=None,
        leadership_management=None, early_years_provision=None,
        sixth_form_provision=None, ungraded_outcome=None, ungraded_grade=None,
        rc_safeguarding_met=None, rc_inclusion=None, rc_curriculum_teaching=None,
        rc_achievement=None, rc_attendance_behaviour=None,
        rc_personal_development=None, rc_leadership_governance=None,
        rc_early_years=None, rc_sixth_form=None, report_url=None,
    )
    base.update(kw)
    return types.SimpleNamespace(**base)


def test_report_card_block_and_provider_url():
    o = _row(rc_achievement=2, rc_inclusion=3, rc_safeguarding_met=True)
    block = _ofsted_block(o, urn=100140)
    assert block["report_card"]["rc_achievement"]["label"] == "Strong standard"
    assert "rc_safeguarding_met" not in block["report_card"]
    assert block["rc_safeguarding_met"] is True
    assert block["ofsted_page_url"] == "https://reports.ofsted.gov.uk/provider/21/100140"


def test_grade_source_graded_vs_carried_forward():
    assert _ofsted_block(_row(overall_effectiveness=1), urn=1)["grade_source"] == "graded"
    carried = _ofsted_block(_row(ungraded_grade=2), urn=1)
    assert carried["grade_source"] == "ungraded_carried_forward"
    assert carried["overall_effectiveness"] == 2
    assert _ofsted_block(_row(), urn=1)["grade_source"] is None


def test_ofsted_block_carries_rc_inspection_date():
    o = _row(
        ungraded_grade=2,
        rc_achievement=1,
        rc_inspection_date=date(2026, 2, 3),
        inspection_date=date(2021, 10, 7),
    )
    block = _ofsted_block(o, urn=138690)
    assert block["rc_inspection_date"] == "2026-02-03"
    # The legacy inspection date is still present, unchanged.
    assert block["inspection_date"] == "2021-10-07"


def test_ofsted_block_rc_inspection_date_none_when_absent():
    o = _row(overall_effectiveness=1, inspection_date=date(2021, 10, 13))
    block = _ofsted_block(o, urn=136276)
    assert block["rc_inspection_date"] is None


def test_ofsted_block_keeps_existing_keys():
    block = _ofsted_block(_row(overall_effectiveness=2, quality_of_education=2), urn=1)
    for key in ("framework", "inspection_date", "overall_effectiveness",
                "quality_of_education", "rc_inclusion", "report_url"):
        assert key in block


def test_admissions_row_new_fields():
    a = types.SimpleNamespace(
        year=202627, school_phase="Primary", places_offered=80,
        total_applications=185, first_preference_applications=74,
        first_preference_offers=74, first_preference_offer_pct=100.0,
        oversubscription_ratio=0.925, oversubscribed=False,
        total_offers=80, second_preference_offers=4, third_preference_offers=2,
        cross_la_applications=12, cross_la_offers=3,
    )
    d = _admissions_row_dict(a)
    for k in ("total_offers", "second_preference_offers", "third_preference_offers",
              "cross_la_applications", "cross_la_offers"):
        assert d[k] == getattr(a, k)
    # Existing keys unchanged
    assert d["first_preference_offer_pct"] == 100.0
    assert d["oversubscribed"] is False
