"""Tests for sync_motion_content.py XML parsers and join builders.

Fixtures use the real SyncFeed XML format:
- Entity ID is an attribute: id="..."
- tk:verwijderd is a namespaced attribute
- zaak refs are child elements with ref="..." attributes
- Zaak onderwerp/soort are child elements with text content
- DocumentVersie uses <document ref="..."/> and <externeIdentifier> child elements
"""

import scripts.sync_motion_content as smc

NS_TK = "http://www.tweedekamer.nl/xsd/tkData/v1-0"
NS_PREFIX = f'xmlns:tk="{NS_TK}" xmlns="{NS_TK}"'


def test_parse_besluit_simple():
    xml = (
        f'<besluit {NS_PREFIX} id="B1" tk:verwijderd="false">'
        '  <zaak ref="Z1" />'
        '  <zaak ref="Z2" />'
        "  <besluitTekst>Aangenomen.</besluitTekst>"
        "</besluit>"
    )
    out = smc.parse_besluit(xml)
    assert out["id"] == "B1"
    assert out["verwijderd"] is False
    assert out["zaak_refs"] == ["Z1", "Z2"]


def test_parse_besluit_deleted():
    xml = (
        f'<besluit {NS_PREFIX} id="B2" tk:verwijderd="true">'
        '  <zaak ref="Z3" />'
        "</besluit>"
    )
    out = smc.parse_besluit(xml)
    assert out["verwijderd"] is True
    assert out["zaak_refs"] == ["Z3"]


def test_parse_zaak_and_title_map():
    zxml = (
        f'<zaak {NS_PREFIX} id="Z1" tk:verwijderd="false">'
        "  <onderwerp>My title</onderwerp>"
        "  <soort>Motie</soort>"
        "</zaak>"
    )
    z = smc.parse_zaak(zxml)
    assert z["id"] == "Z1"
    assert z["verwijderd"] is False
    assert z["onderwerp"] == "My title"
    assert z["soort"] == "Motie"

    besluit_index = {"B1": {"zaak_refs": ["Z1"]}}
    zaak_index = {"Z1": z}
    tm = smc.build_title_map(besluit_index, zaak_index)
    assert tm["B1"] == "My title"


def test_build_title_map_prefers_motie():
    """When a Besluit links multiple Zaak records, prefer soort==Motie."""
    zaak_index = {
        "Z1": {
            "id": "Z1",
            "verwijderd": False,
            "onderwerp": "Other title",
            "soort": "Amendement",
        },
        "Z2": {
            "id": "Z2",
            "verwijderd": False,
            "onderwerp": "Motion title",
            "soort": "Motie",
        },
    }
    besluit_index = {"B1": {"zaak_refs": ["Z1", "Z2"]}}
    tm = smc.build_title_map(besluit_index, zaak_index)
    assert tm["B1"] == "Motion title"


def test_parse_document():
    dxml = (
        f'<document {NS_PREFIX} id="D1" tk:verwijderd="false">'
        '  <zaak ref="Z1" />'
        "</document>"
    )
    doc = smc.parse_document(dxml)
    assert doc["id"] == "D1"
    assert doc["verwijderd"] is False
    assert doc["zaak_refs"] == ["Z1"]


def test_parse_documentversie():
    dvxml = (
        f'<documentVersie {NS_PREFIX} id="DV1" tk:verwijderd="false">'
        '  <document ref="D1" />'
        "  <externeIdentifier>kst-12345-678</externeIdentifier>"
        "  <extensie>html</extensie>"
        "</documentVersie>"
    )
    dv = smc.parse_documentversie(dvxml)
    assert dv["id"] == "DV1"
    assert dv["verwijderd"] is False
    assert dv["document_id"] == "D1"
    assert dv["externe_identifier"] == "kst-12345-678"
    assert dv["extensie"] == "html"


def test_parse_document_and_docversie_and_extid_map():
    dxml = (
        f'<document {NS_PREFIX} id="D1" tk:verwijderd="false">'
        '  <zaak ref="Z1" />'
        "</document>"
    )
    dvxml = (
        f'<documentVersie {NS_PREFIX} id="DV1" tk:verwijderd="false">'
        '  <document ref="D1" />'
        "  <externeIdentifier>EXT-123</externeIdentifier>"
        "  <extensie>html</extensie>"
        "</documentVersie>"
    )
    doc = smc.parse_document(dxml)
    dv = smc.parse_documentversie(dvxml)
    besluit_index = {"B1": {"zaak_refs": ["Z1"]}}
    zaak_index = {"Z1": {"id": "Z1", "onderwerp": "t", "soort": "Motie"}}
    doc_index = {"D1": doc}
    dv_index = {"DV1": dv}
    extmap = smc.build_ext_id_map(besluit_index, zaak_index, doc_index, dv_index)
    assert extmap["B1"] == "EXT-123"
