"""Danh mục thiết bị để chọn trong hộp thoại 'Thêm thiết bị': thiết bị thật + các model giả lập (gộp một danh sách)."""
from __future__ import annotations

from dataclasses import dataclass

from .vendors import VENDORS, vendor_labels

HEADER_REAL = "━━━━━  THIẾT BỊ THẬT  ━━━━━"
HEADER_SIM = "━━━━━  🧪 THIẾT BỊ GIẢ LẬP (không cần thiết bị thật)  ━━━━━"


@dataclass(frozen=True)
class Choice:
    label: str
    vendor: str = ""
    model: str = ""
    simulated: bool = False
    header: bool = False


# (khóa hãng, tên hiển thị, mã model) - mỗi dòng là một thiết bị giả lập chọn được
SIM_MODELS: list[tuple[str, str, str]] = [
    # ---- Juniper ----
    ("juniper_junos", "Juniper EX2300-24T (Junos)", "ex2300-24t"),
    ("juniper_junos", "Juniper EX3400-48T (Junos)", "ex3400-48t"),
    ("juniper_junos", "Juniper EX4300-48T (Junos)", "ex4300-48t"),
    ("juniper_junos", "Juniper EX4400-48P (Junos)", "ex4400-48p"),
    ("juniper_junos", "Juniper QFX5100-48S (Junos)", "qfx5100-48s"),
    ("juniper_srx", "Juniper SRX300 (firewall)", "srx300"),
    ("juniper_srx", "Juniper SRX320 (firewall)", "srx320"),
    ("juniper_srx", "Juniper SRX340 (firewall)", "srx340"),
    ("juniper_srx", "Juniper SRX1500 (firewall)", "srx1500"),
    ("juniper_srx", "Juniper vSRX (firewall ảo)", "vsrx"),
    # ---- Cisco ----
    ("cisco_ios", "Cisco Catalyst 2960-X 48 cổng (IOS)", "WS-C2960X-48TS-L"),
    ("cisco_ios", "Cisco Catalyst 3750-X (IOS)", "WS-C3750X-48P"),
    ("cisco_ios", "Cisco Catalyst 3850 (IOS-XE cũ)", "WS-C3850-24T"),
    ("cisco_xe", "Cisco Catalyst 9200 (IOS-XE)", "C9200-24T"),
    ("cisco_xe", "Cisco Catalyst 9300 (IOS-XE)", "C9300-48P"),
    ("cisco_xe", "Cisco ISR 4331 Router (IOS-XE)", "ISR4331"),
    ("cisco_nxos", "Cisco Nexus 3048 (NX-OS)", "N3K-C3048TP"),
    ("cisco_nxos", "Cisco Nexus 9300 (NX-OS)", "N9K-C93180YC-EX"),
    ("cisco_s300", "Cisco SG350-28 (Small Business)", "SG350-28"),
    ("cisco_ap", "Cisco Aironet 2702 AP (autonomous)", "AIR-CAP2702I"),
    # ---- Aruba / HPE ----
    ("aruba_os", "Aruba 2930F-48G (ArubaOS-Switch)", "JL254A"),
    ("aruba_os", "Aruba 2530-24G (ArubaOS-Switch)", "J9776A"),
    ("hp_procurve", "HPE ProCurve 2610 (cũ)", "J9085A"),
    ("hp_comware", "HPE 5130 EI (Comware)", "JG932A"),
    ("hp_comware", "H3C S5120 (Comware)", "S5120-28P"),
    # ---- Huawei ----
    ("huawei", "Huawei S5735-L24T4S (VRP)", "S5735-L24T4S-A"),
    ("huawei", "Huawei S5720-52X (VRP)", "S5720-52X-LI"),
    ("huawei", "Huawei S6730-H (VRP)", "S6730-H24X6C"),
    # ---- MikroTik ----
    ("mikrotik_routeros", "MikroTik hEX RB750Gr3", "RB750Gr3"),
    ("mikrotik_routeros", "MikroTik CRS326-24G-2S+ (switch)", "CRS326-24G-2S+"),
    ("mikrotik_routeros", "MikroTik hAP ac2 (AP/router)", "RBD52G-5HacD2HnD"),
    # ---- Wi-Fi / Linux ----
    ("openwrt", "OpenWrt AP/Router (TP-Link Archer C7)", "archer-c7"),
    # ---- Khác ----
    ("tplink_jetstream", "TP-Link JetStream TL-SG3428", "TL-SG3428"),
    ("fortinet", "Fortinet FortiSwitch 124F", "FS-124F"),
    ("fortinet", "Fortinet FortiGate 60F", "FG-60F"),
    ("ubiquiti_edgeswitch", "Ubiquiti EdgeSwitch 24", "ES-24-250W"),
    ("ubiquiti_unifiswitch", "Ubiquiti UniFi Switch 24 (CLI)", "US-24"),
    ("ruijie_os", "Ruijie RG-S5750 (RGOS)", "RG-S5750-24GT"),
    ("dell_os10", "Dell EMC S4048 (OS10)", "S4048-ON"),
    ("dell_powerconnect", "Dell PowerConnect 6224", "PC6224"),
    ("extreme_exos", "Extreme X450-G2 (EXOS)", "X450G2-48p"),
    ("ruckus_fastiron", "Ruckus ICX 7150 (FastIron)", "ICX7150-24"),
    ("zyxel_os", "Zyxel GS1920-24", "GS1920-24"),
    ("allied_telesis_awplus", "Allied Telesis x230 (AlliedWare Plus)", "AT-x230-28GP"),
    ("alcatel_aos", "Alcatel-Lucent OmniSwitch 6860", "OS6860E-24"),
    ("linux", "Linux / thiết bị SSH chung", "linux"),
]


def build_choices() -> list[Choice]:
    """Danh sách gộp: thiết bị thật (theo hãng) rồi tới các thiết bị giả lập (theo model)."""
    out = [Choice(HEADER_REAL, header=True)]
    out += [Choice(label, vendor=key) for key, label in vendor_labels(include_mock=False)]
    out.append(Choice(HEADER_SIM, header=True))
    out += [Choice(f"🧪 {disp}   [giả lập]", vendor=v, model=m, simulated=True) for v, disp, m in SIM_MODELS]
    return out


def find_choice(choices: list[Choice], vendor: str, simulated: bool, model: str = "") -> Choice:
    """Tìm mục tương ứng với thiết bị đã lưu (dùng khi sửa thiết bị)."""
    real = [c for c in choices if not c.header and c.simulated == bool(simulated) and c.vendor == vendor]
    if simulated and model:
        for c in real:
            if c.model == model:
                return c
    if real:
        return real[0]
    fallback = [c for c in choices if not c.header and c.vendor == vendor]
    return fallback[0] if fallback else next(c for c in choices if not c.header)


def filter_choices(choices: list[Choice], text: str) -> list[Choice]:
    """Lọc theo từ khóa (không phân biệt hoa/thường); luôn giữ các dòng tiêu đề nhóm."""
    t = text.strip().lower()
    if not t:
        return choices
    return [c for c in choices if c.header or t in c.label.lower() or t in c.vendor.lower() or t in c.model.lower()]


assert all(v in VENDORS for v, _, _ in SIM_MODELS), "SIM_MODELS có khóa hãng không tồn tại"
