"""
Known Adnacom Monitor H3 / PEX8718 boards (PCI BDF, board serial, SFP serials per lane).

Populated from operator screenshots; used during panel calibrate so PCIe metadata can be
chosen with number keys (no copy/paste).
"""

from typing import Any, Dict, List

# SFP order is monitor "SFP 1" … "SFP 4" (adapter_port 1–4).
ADNACOM_KNOWN_CARDS: List[Dict[str, Any]] = [
    {
        "bus_address": "0000:01:00.0",
        "board_label": "H3",
        "board_serial": "4C-0A-3D-B2-13-13",
        "switch_chip": "PEX8718",
        "slot_port": 0,
        "link_capability": "Gen3 x4",
        "link_state": "Gen3 x4",
        "sfp_serials": [
            "6C82510221225",
            "6C82510221222",
            "6C82510221223",
            "6C82510221224",
        ],
    },
    {
        "bus_address": "0000:07:00.0",
        "board_label": "H3",
        "board_serial": "4C-0A-3D-B2-13-19",
        "switch_chip": "PEX8718",
        "slot_port": 0,
        "link_capability": "Gen3 x4",
        "link_state": "Gen3 x4",
        "sfp_serials": [
            "6C82510221226",
            "6C82601120819",
            "6C82512030665",
            "6C82601120820",
        ],
    },
    {
        "bus_address": "0000:21:00.0",
        "board_label": "H3",
        "board_serial": "4C-0A-3D-02-13-15",
        "switch_chip": "PEX8718",
        "slot_port": 0,
        "link_capability": "Gen3 x4",
        "link_state": "Gen3 x4",
        "sfp_serials": [
            "6C82601120813",
            "6C82601120811",
            "6C82601120814",
            "6C82601120816",
        ],
    },
    {
        "bus_address": "0000:c1:00.0",
        "board_label": "H3",
        "board_serial": "4C-0A-3D-5B-13-03",
        "switch_chip": "PEX8718",
        "slot_port": 0,
        "link_capability": "Gen3 x4",
        "link_state": "Gen3 x4",
        "sfp_serials": [
            "6C82510221389",
            "6C82510221388",
            "6C82510221387",
            "6C82510221386",
        ],
    },
    {
        "bus_address": "0000:c7:00.0",
        "board_label": "H3",
        "board_serial": "4C-0A-3D-02-13-1A",
        "switch_chip": "PEX8718",
        "slot_port": 0,
        "link_capability": "Gen3 x4",
        "link_state": "Gen3 x4",
        "sfp_serials": [
            "6C82601120815",
            "6C82601120818",
            "6C82512030663",
            "6C82601120817",
        ],
    },
    {
        "bus_address": "0000:e2:00.0",
        "board_label": "H3",
        "board_serial": "4C-0A-3D-02-13-17",
        "switch_chip": "PEX8718",
        "slot_port": 0,
        "link_capability": "Gen3 x4",
        "link_state": "Gen3 x4",
        "sfp_serials": [
            "6C82510221229",
            "6C82510221230",
            "6C82510221228",
            "6C82510221227",
        ],
    },
]


def short_bdf(bus_address: str) -> str:
    s = (bus_address or "").strip()
    if s.startswith("0000:"):
        return s[5:]
    return s


def board_serial_tail(board_serial: str) -> str:
    s = (board_serial or "").strip()
    if len(s) <= 10:
        return s
    return s[-10:]


def print_adnacom_host_card_table() -> None:
    """Print known Adnacom PCIe host cards from the built-in catalog (operator reference)."""
    print("Adnacom host cards (known catalog — H3 / PEX8718)", flush=True)
    print("-" * 72, flush=True)
    for i, c in enumerate(ADNACOM_KNOWN_CARDS, start=1):
        bdf = short_bdf(c["bus_address"])
        tail = board_serial_tail(c["board_serial"])
        chip = c.get("switch_chip", "")
        print(
            f"  {i:>2}  {bdf:>10}  {c['board_label']:>4}  board SN …{tail}  {chip}",
            flush=True,
        )
    print("-" * 72, flush=True)


def print_catalog_menu() -> None:
    print("  Known Adnacom H3 / PEX8718 cards (match the dropdown in Adnacom Monitor):", flush=True)
    for i, c in enumerate(ADNACOM_KNOWN_CARDS, start=1):
        bdf = short_bdf(c["bus_address"])
        tail = board_serial_tail(c["board_serial"])
        print(
            f"    {i}) {bdf:>8}  {c['board_label']}  board SN …{tail}",
            flush=True,
        )
    print("  m) Type PCIe fields manually (long-form prompts)", flush=True)


def pcie_from_card_and_lane(card: Dict[str, Any], sfp_lane_1_4: int) -> Dict[str, Any]:
    if sfp_lane_1_4 < 1 or sfp_lane_1_4 > 4:
        raise ValueError("sfp_lane_1_4 must be 1–4")
    serials = card["sfp_serials"]
    serial = serials[sfp_lane_1_4 - 1]
    return {
        "bus_address": card["bus_address"],
        "board_label": card["board_label"],
        "board_serial": card["board_serial"],
        "switch_chip": card["switch_chip"],
        "slot_port": int(card["slot_port"]),
        "link_capability": card["link_capability"],
        "link_state": card["link_state"],
        "adapter_port": sfp_lane_1_4,
        "sfp_serial": serial,
    }
