"""Vendor-specific OLT field contracts.

Profiles describe field requirements and execution readiness.  They do not
open network connections.  A profile marked unsupported is deliberately
fail-closed until its real adapter is implemented and tested.
"""
from dataclasses import dataclass


@dataclass(frozen=True)
class OltVendorProfile:
    key: str
    vendor: str
    model: str
    label: str
    execution_state: str  # preview_supported, live_supported, unsupported
    discovery_state: str  # unavailable, grammar_unverified, ready
    fields: tuple[dict[str, str], ...]


_COMMON = (
    {"key": "pon_port", "label": "Port PON", "type": "text", "required": "true", "placeholder": "contoh: 1/1/1"},
    {"key": "ont_sn", "label": "SN ONT", "type": "text", "required": "true", "placeholder": "serial ONT"},
    {"key": "onu_id", "label": "ONU ID", "type": "number", "required": "true", "placeholder": "ID ONU"},
    {"key": "service_vlan", "label": "Service VLAN", "type": "number", "required": "true", "placeholder": "175"},
    {"key": "wan_vlan", "label": "WAN VLAN", "type": "number", "required": "true", "placeholder": "175"},
    {"key": "auth_mode", "label": "Mode autentikasi", "type": "select", "required": "true", "options": "sn,password,loid"},
)

_C_DATA_DISCOVERY_FIELDS: tuple[dict[str, str], ...] = ()

_PROFILES = (
    OltVendorProfile("zte-c320", "ZTE", "C320", "ZTE C320", "preview_supported", "unavailable", _COMMON + (
        {"key": "line_profile", "label": "Line Profile", "type": "text", "required": "true", "placeholder": "line profile ZTE"},
        {"key": "service_profile", "label": "Service Profile", "type": "text", "required": "true", "placeholder": "service profile ZTE"},
    )),
    OltVendorProfile("cdata-fd16", "C-DATA", "FD16", "C-DATA FD16", "unsupported", "unavailable", _COMMON + (
        {"key": "onu_type", "label": "Tipe ONU", "type": "text", "required": "true", "placeholder": "tipe ONU C-DATA"},
    )),
    OltVendorProfile("cdata-fd1601s-b1", "C-DATA", "FD1601S-B1", "C-DATA FD1601S-B1", "live_supported", "ready", _C_DATA_DISCOVERY_FIELDS),
    OltVendorProfile("huawei-ma5800", "Huawei", "MA5800", "Huawei MA5800", "unsupported", "unavailable", _COMMON + (
        {"key": "ont_line_profile", "label": "ONT Line Profile", "type": "text", "required": "true", "placeholder": "profile Huawei"},
        {"key": "ont_srv_profile", "label": "ONT Service Profile", "type": "text", "required": "true", "placeholder": "profile Huawei"},
    )),
    OltVendorProfile("fiberhome-an5516", "FiberHome", "AN5516", "FiberHome AN5516", "unsupported", "unavailable", _COMMON + (
        {"key": "template_name", "label": "Template layanan", "type": "text", "required": "true", "placeholder": "template FiberHome"},
    )),
    OltVendorProfile("nokia-isam", "Nokia", "ISAM", "Nokia ISAM", "unsupported", "unavailable", _COMMON + (
        {"key": "ont_profile", "label": "ONT Profile", "type": "text", "required": "true", "placeholder": "profile Nokia"},
    )),
    # Generic scaffolds intentionally remain fail-closed until vendor syntax is validated.
    OltVendorProfile("hsgq-generic", "HSGQ", "Generic", "HSGQ (Generic)", "unsupported", "unavailable", _COMMON + (
        {"key": "line_profile", "label": "Line Profile", "type": "text", "required": "true", "placeholder": "profile HSGQ"},
        {"key": "service_profile", "label": "Service Profile", "type": "text", "required": "true", "placeholder": "profile HSGQ"},
    )),
    OltVendorProfile("vsol-generic", "VSOL", "Generic", "VSOL (Generic)", "unsupported", "unavailable", _COMMON + (
        {"key": "line_profile", "label": "Line Profile", "type": "text", "required": "true", "placeholder": "profile VSOL"},
        {"key": "service_profile", "label": "Service Profile", "type": "text", "required": "true", "placeholder": "profile VSOL"},
    )),
    OltVendorProfile("global-generic", "GLOBAL", "Generic", "GLOBAL (Generic)", "unsupported", "unavailable", _COMMON + (
        {"key": "line_profile", "label": "Line Profile", "type": "text", "required": "true", "placeholder": "profile GLOBAL"},
        {"key": "service_profile", "label": "Service Profile", "type": "text", "required": "true", "placeholder": "profile GLOBAL"},
    )),
)
_BY_KEY = {profile.key: profile for profile in _PROFILES}
_BY_VENDOR_MODEL = {(profile.vendor.casefold(), profile.model.casefold()): profile for profile in _PROFILES}


def get_vendor_profile(vendor: str | None, model: str | None) -> OltVendorProfile | None:
    return _BY_VENDOR_MODEL.get(((vendor or "").strip().casefold(), (model or "").strip().casefold()))


def get_vendor_profile_by_key(key: str | None) -> OltVendorProfile | None:
    return _BY_KEY.get((key or "").strip())


def vendor_profiles_for_ui() -> list[dict]:
    return [
        {"key": p.key, "vendor": p.vendor, "model": p.model, "label": p.label,
         "execution_state": p.execution_state, "discovery_state": p.discovery_state,
         "fields": list(p.fields)}
        for p in _PROFILES
    ]
