import xml.etree.ElementTree as ET

tree = ET.parse("boardloaf-he/netlist.xml")
root = tree.getroot()

components = {}
for comp in root.findall("./components/comp"):
    ref = comp.attrib["ref"]
    value = comp.find("value").text if comp.find("value") is not None else ""
    lib = (
        comp.find("libsource").attrib["lib"]
        if comp.find("libsource") is not None
        else ""
    )
    part = (
        comp.find("libsource").attrib["part"]
        if comp.find("libsource") is not None
        else ""
    )
    components[ref] = {"value": value, "lib": lib, "part": part, "nets": {}}

for net in root.findall("./nets/net"):
    net_name = net.attrib["name"]
    for node in net.findall("node"):
        ref = node.attrib["ref"]
        pin = node.attrib["pin"]
        if ref in components:
            components[ref]["nets"][pin] = net_name

# Print some analysis
for ref, data in components.items():
    if "RP2040" in data["part"]:
        print(f"--- MCU: {ref} ({data['part']}) ---")
        for pin, net in data["nets"].items():
            if "Net" not in net and net != "unconnected":  # simplify output
                print(f"  Pin {pin}: {net}")

    if "74HC4051" in data["part"]:
        print(f"--- MUX: {ref} ({data['part']}) ---")
        for pin, net in data["nets"].items():
            print(f"  Pin {pin}: {net}")

    if "SLSS49E" in data["part"]:
        pass  # Will analyze these in bulk

print("--- Checking SS49E power ---")
ss49e_bad_power = []
for ref, data in components.items():
    if "SLSS49E" in data["part"]:
        # SLSS49E pins: 1: VCC, 2: GND, 3: OUT (typically)
        # SOT-23 usually: 1=VCC, 2=OUT, 3=GND or 1=VCC, 2=GND, 3=OUT. We need to check symbol
        pass
