"""Single-flow PSB onboarding: MikroTik secret + supported OLT WAN."""

from dataclasses import dataclass
import asyncio
import json
import logging
_customer_log = logging.getLogger(__name__)
import os
from datetime import datetime, timedelta, timezone
from sqlalchemy import select, func

from app.core.customer_identity import decrypt_pppoe_password
from app.core.acs_management import MANAGEMENT_VLAN, MANAGEMENT_GEMPORT, MANAGEMENT_MAPPING_INDEX
from app.core.device_credentials import decrypt_secret
from app.core.mikrotik import RealMikrotikAdapter, MikrotikProvisioningError
from app.core.cdata_executor import CDataExecutionError, provision_cdata_onu, provision_cdata_fd1602_onu, provision_cdata_pppoe, read_cdata_ont_optical_power, verify_cdata_fd1602_onu, verify_cdata_onu, reboot_cdata_onu
from app.core.genieacs import (
    GenieACSUnavailable,
    configure_pppoe_wan,
    sync_psb_internet_wan,
    get_customer_device,
    get_wifi_configuration,
    verify_default_pppoe_route,
)
from app.models.models import Customer, DeviceCredential, MikrotikDevice, OLTDevice, ProvisioningEvent

_ONBOARDING_LOCKS: dict[int, asyncio.Lock] = {}
# OLT CLI sessions are stateful and some devices close a concurrent session.
# Keep live probes for the same OLT serialised even when the status worker,
# provisioning worker, and a detail-page refresh overlap.
_LIVE_OLT_PROBE_LOCKS: dict[int, asyncio.Lock] = {}


def classify_live_connectivity(*, olt_online: bool | None, mikrotik_reachable: bool,
                               secret_enabled: bool | None, pppoe_active: bool | None,
                               internet_reachable: bool | None, errors: list[str]) -> tuple[str, str]:
    """Classify only from observed evidence; never label an unavailable probe offline."""
    if errors or olt_online is None or not mikrotik_reachable or secret_enabled is None or pppoe_active is None:
        detail = "; ".join(errors) if errors else "Probe OLT atau MikroTik belum lengkap."
        return "unknown", f"Status internet belum bisa dipastikan. {detail}"
    if not olt_online:
        return "offline", "ONU terverifikasi offline di OLT."
    if not secret_enabled:
        return "offline", "Secret PPPoE tidak aktif atau tidak ditemukan di MikroTik."
    if not pppoe_active:
        return "waiting_session", "Secret PPPoE aktif, tetapi sesi untuk username ini tidak terlihat di MikroTik. Status internet pelanggan belum dapat disimpulkan dari pemeriksaan ini."
    if internet_reachable is True:
        return "active", "OLT mengonfirmasi ONT online, sesi PPPoE aktif, dan MikroTik dapat menjangkau upstream Internet."
    return "waiting_internet", "Sesi PPPoE aktif, tetapi MikroTik belum dapat memverifikasi koneksi upstream Internet. BestWeb akan memeriksa ulang otomatis."


@dataclass
class OnboardingResult:
    mikrotik: str
    olt: str
    message: str


async def _notify(on_stage, name: str, percent: int, state: str, message: str) -> None:
    if on_stage is not None:
        await on_stage(name, percent, state, message)


async def _mikrotik(customer: Customer, db, on_stage=None) -> tuple[str, str]:
    stage = "Mengatur secret PPPoE di MikroTik"
    verify_stage = None
    await _notify(on_stage, stage, 70, "processing", "Menghubungkan ke MikroTik dan menerapkan profil layanan.")
    if not all((customer.mikrotik_device_id, customer.pppoe_username, customer.pppoe_password)):
        return "pending_configuration", "Device MikroTik atau kredensial PPPoE belum lengkap."
    device = await db.get(MikrotikDevice, customer.mikrotik_device_id)
    if not device or not device.is_active or not device.credential_id:
        return "pending_configuration", "Device MikroTik belum aktif atau credential belum terikat."
    credential = await db.get(DeviceCredential, device.credential_id)
    if not credential or not credential.is_active:
        return "pending_configuration", "Credential MikroTik belum aktif."
    try:
        adapter = RealMikrotikAdapter(
            host=device.host,
            username=credential.username,
            password=decrypt_secret(credential.encrypted_secret),
            api_port=device.api_port or 8728,
        )
        profile, used_default = await asyncio.to_thread(adapter.resolve_profile, customer.service_profile)
        await asyncio.to_thread(
            adapter.upsert_pppoe_secret,
            customer.pppoe_username,
            decrypt_pppoe_password(customer.pppoe_password),
            profile,
        )
        await _notify(on_stage, stage, 70, "success", f"Secret PPPoE aktif pada {device.name}.")
        verify_stage = "Memeriksa sesi PPPoE dan upstream MikroTik"
        await _notify(on_stage, verify_stage, 85, "processing", "Secret aktif; memeriksa sesi PPPoE pelanggan.")
        enabled, active, internet = await asyncio.to_thread(adapter.verify_pppoe_internet, customer.pppoe_username)
        if not enabled:
            await _notify(on_stage, verify_stage, 85, "failed", "Secret PPPoE tidak terverifikasi aktif di MikroTik.")
            return "failed", "Secret PPPoE tidak terverifikasi aktif di MikroTik."
        if not active:
            await _notify(on_stage, verify_stage, 85, "waiting", "Menunggu sesi PPPoE dari ONT.")
            return "waiting_session", f"Secret PPPoE aktif pada {device.name}; menunggu sesi pelanggan."
        suffix = " Profile default dipakai." if used_default else ""
        if not internet:
            message = f"Sesi PPPoE aktif pada {device.name}, tetapi MikroTik belum dapat menjangkau upstream Internet. BestWeb melanjutkan pemeriksaan otomatis.{suffix}"
            await _notify(on_stage, verify_stage, 90, "waiting", message)
            return "waiting_internet", message
        await _notify(on_stage, verify_stage, 90, "success", "Sesi PPPoE aktif dan MikroTik menjangkau upstream Internet.")
        return "provisioned", f"Sesi PPPoE aktif dan koneksi upstream Internet terverifikasi pada {device.name}.{suffix}"
    except (MikrotikProvisioningError, ValueError, RuntimeError) as exc:
        failed_stage = verify_stage or stage
        await _notify(on_stage, failed_stage, 85 if verify_stage else 70, "failed", "Provisioning atau verifikasi MikroTik gagal. Periksa detail teknis pada log server.")
        return "failed", f"MikroTik gagal: {str(exc)[:300]}"
    except Exception:
        failed_stage = verify_stage or stage
        await _notify(on_stage, failed_stage, 85 if verify_stage else 70, "failed", "Provisioning MikroTik gagal karena koneksi atau credential.")
        return "failed", "MikroTik gagal karena koneksi atau credential tidak valid."



async def sync_acs_enrollment(customer: Customer, db, on_stage=None) -> dict:
    """Record the real ACS enrollment state without fabricating a device record.

    GenieACS learns an ONT only from the ONT's own Inform.  The OLT stage
    prepares management reachability; this helper then monitors the actual
    enrollment and reads available WLAN metadata after the first Inform.
    """
    stage = "Sinkronisasi GenieACS"
    if not customer.ont_sn:
        return {"registered": False, "connected": False, "message": "Serial ONT belum tersedia."}
    try:
        device = await asyncio.to_thread(get_customer_device, customer.ont_sn)
    except GenieACSUnavailable as exc:
        customer.acs_last_sync = datetime.now(timezone.utc)
        await _notify(on_stage, stage, 60, "waiting", "GenieACS sementara tidak dapat dijangkau; sinkronisasi akan dicoba ulang otomatis.")
        return {"registered": False, "connected": False, "message": str(exc)[:240]}
    if device is None:
        customer.acs_wifi_status = "waiting_inform"
        customer.acs_last_sync = datetime.now(timezone.utc)
        await _notify(on_stage, stage, 60, "waiting", "Manajemen ONT sudah disiapkan; menunggu Inform pertama dari ONT ke GenieACS.")
        return {"registered": False, "connected": False, "message": "Menunggu Inform pertama ONT."}
    last_inform = device.get("last_inform")
    try:
        inform_at = datetime.fromisoformat(str(last_inform).replace("Z", "+00:00"))
        if inform_at.tzinfo is None:
            inform_at = inform_at.replace(tzinfo=timezone.utc)
        inform_is_fresh = datetime.now(timezone.utc) - inform_at.astimezone(timezone.utc) <= timedelta(minutes=15)
    except (TypeError, ValueError):
        inform_is_fresh = False
    if not inform_is_fresh:
        customer.acs_wifi_status = "waiting_inform"
        customer.acs_last_sync = datetime.now(timezone.utc)
        await _notify(on_stage, stage, 60, "waiting", "ONT terdaftar di GenieACS, tetapi Inform belum terbaru; menunggu Inform baru.")
        return {"registered": True, "connected": False, "message": "Menunggu Inform terbaru dari ONT."}
    try:
        wifi = await asyncio.to_thread(get_wifi_configuration, customer.ont_sn)
    except GenieACSUnavailable:
        wifi = {"data": []}
    customer.acs_last_sync = datetime.now(timezone.utc)
    if customer.acs_wifi_status not in {"desired", "queued", "pending_verification"}:
        customer.acs_wifi_status = "available" if wifi.get("data") else "connected"
    await _notify(on_stage, stage, 60, "success", "ONT sudah mengirim Inform ke GenieACS; data ACS tersedia.")
    return {"registered": True, "connected": True, "wifi_available": bool(wifi.get("data"))}


async def _sync_acs_pppoe_wan(customer: Customer, db, on_stage=None) -> dict:
    """Configure ACS only after the ONT has enrolled through its own Inform."""
    enrollment = await sync_acs_enrollment(customer, db, on_stage=on_stage)
    if not enrollment.get("registered"):
        return enrollment
    try:
        result = await asyncio.to_thread(
            configure_pppoe_wan, customer.ont_sn,
            username=customer.pppoe_username,
            password=decrypt_pppoe_password(customer.pppoe_password),
            vlan=customer.service_vlan or customer.pppoe_vlan or 100,
            wait_seconds=0, reuse_vlan=True,
        )
        return {**enrollment, **result}
    except Exception as exc:
        return {**enrollment, "connected": False, "message": str(exc)[:240]}


def psb_wan_sync_supported(customer: Customer) -> bool:
    return ((customer.olt_vendor or "").strip().upper() == "C-DATA"
            and (customer.olt_model or "").strip().upper() == "FD1601S-B1"
            and (customer.service_mode or customer.provisioning_mode or "").strip().lower() == "private_pppoe"
            and (getattr(customer, "play_type", None) or "1play").strip().lower() == "1play"
            and (customer.ont_sn or "").strip().upper().startswith("ZTE"))


async def sync_psb_customer_wan(customer: Customer, db) -> dict | None:
    """Synchronize only the Internet WAN, independently of manual ACS setup."""
    if not psb_wan_sync_supported(customer):
        return None
    serial = (customer.ont_sn or "").strip().upper()
    duplicate = await db.scalar(select(Customer.id).where(
        Customer.id != customer.id,
        func.coalesce(Customer.is_archived, False) == False,
        func.upper(func.trim(Customer.ont_sn)) == serial,
    ).limit(1))
    if duplicate is not None:
        return {"ready": False, "state": "failed",
                "message": "Serial ONT juga dipakai pelanggan lain; sinkronisasi WAN ditahan."}
    if not customer.pppoe_username or not customer.pppoe_password:
        return {"ready": False, "state": "failed", "message": "Kredensial PPPoE pelanggan belum lengkap."}
    secret = ""
    try:
        secret = decrypt_pppoe_password(customer.pppoe_password)
        return await asyncio.to_thread(
            sync_psb_internet_wan, serial, username=customer.pppoe_username,
            password=secret, vlan=customer.service_vlan or customer.pppoe_vlan or 100,
            customer_created_at=customer.created_at,
        )
    except GenieACSUnavailable as exc:
        message = str(exc).replace(secret, "[redacted]") if secret else str(exc)
        return {"ready": False, "state": "failed", "message": message[:300]}
    except Exception:
        return {"ready": False, "state": "waiting",
                "message": "Pembacaan ONT sementara belum tersedia; pemeriksaan akan dilanjutkan."}


async def _olt(customer: Customer, db, on_stage=None) -> tuple[str, str]:
    olt_stage = "Mengonfigurasi dan memverifikasi ONT/OLT"
    required = (customer.olt_device_id, customer.ont_sn, customer.olt_port, customer.onu_id)
    if not all(required):
        return "pending_configuration", "OLT belum lengkap: device, SN ONT, port PON, dan ONU ID wajib."
    if customer.olt_vendor != "C-DATA" or customer.olt_model not in {"FD1601S-B1", "FD1602S-B1"}:
        return "pending_configuration", "Executor otomatis belum tersedia untuk model OLT ini."
    olt = await db.get(OLTDevice, customer.olt_device_id)
    if not olt or not olt.is_active or olt.management_protocol != "ssh" or not olt.credential_id:
        return "pending_configuration", "OLT SSH belum aktif atau credential belum terikat."
    credential = await db.get(DeviceCredential, olt.credential_id)
    pinned = os.getenv(f"OLT_{olt.id}_SSH_HOST_KEY_SHA256")
    if not credential or not credential.is_active or not pinned:
        return "pending_configuration", "Credential atau fingerprint host-key OLT belum siap."
    management_vlan = MANAGEMENT_VLAN
    try:
        if customer.olt_model == "FD1602S-B1":
            result = await asyncio.to_thread(
                provision_cdata_fd1602_onu,
                host=olt.host, port=olt.api_port or 22, username=credential.username,
                encrypted_secret=credential.encrypted_secret, pinned_host_key=pinned,
                pon_port=customer.olt_port, onu_id=customer.onu_id, serial_number=customer.ont_sn,
                pppoe_username=customer.pppoe_username,
                pppoe_password=decrypt_pppoe_password(customer.pppoe_password),
                vlan=customer.service_vlan or customer.pppoe_vlan or 100,
                management_vlan=management_vlan,
                management_gemport=MANAGEMENT_GEMPORT,
                management_mapping_index=MANAGEMENT_MAPPING_INDEX,
            )
            acs_note = " Konfigurasi GenieACS/TR-069 tersedia secara manual dari Detail Pelanggan."
            if getattr(result, "runtime_ready", None) is False:
                return "waiting_session", result.message + acs_note
            return "provisioned", result.message + acs_note
        await asyncio.to_thread(
            provision_cdata_onu,
            host=olt.host, port=olt.api_port or 22, username=credential.username,
            encrypted_secret=credential.encrypted_secret, pinned_host_key=pinned,
            pon_port=customer.olt_port, onu_id=customer.onu_id, serial_number=customer.ont_sn,
            profile_name="2p",
        )
        result = await asyncio.to_thread(
            provision_cdata_pppoe,
            host=olt.host, port=olt.api_port or 22, username=credential.username,
            encrypted_secret=credential.encrypted_secret, pinned_host_key=pinned,
            pon_port=customer.olt_port, onu_id=customer.onu_id, serial_number=customer.ont_sn,
            pppoe_username=customer.pppoe_username,
            pppoe_password=decrypt_pppoe_password(customer.pppoe_password),
            vlan=customer.service_vlan or customer.pppoe_vlan or 100,
            management_vlan=management_vlan,
            management_gemport=MANAGEMENT_GEMPORT,
            management_mapping_index=MANAGEMENT_MAPPING_INDEX,
            management_only=psb_wan_sync_supported(customer),
        )
        verified = await asyncio.to_thread(
            verify_cdata_onu,
            host=olt.host, port=olt.api_port or 22, username=credential.username,
            encrypted_secret=credential.encrypted_secret, pinned_host_key=pinned,
            pon_port=customer.olt_port, onu_id=customer.onu_id, serial_number=customer.ont_sn,
        )
        if not verified:
            return "failed", "OLT tidak mengonfirmasi SN/ONU setelah command diterima."
        acs_note = " Konfigurasi GenieACS/TR-069 tersedia secara manual dari Detail Pelanggan."
        if result.runtime_ready is False:
            if psb_wan_sync_supported(customer):
                return "waiting_session", "ONU terdaftar; menunggu sinkronisasi WAN Internet pada ONT."
            attempted_at = customer.wan_recovery_reboot_attempted_at
            if attempted_at is not None and attempted_at.tzinfo is None:
                attempted_at = attempted_at.replace(tzinfo=timezone.utc)
            now = datetime.now(timezone.utc)
            if attempted_at is None or now - attempted_at >= timedelta(minutes=30):
                await _notify(on_stage, olt_stage, 45, "processing", "WAN tersimpan tetapi ONT belum menjalankannya; BestWeb menerapkan ulang dengan satu reboot otomatis.")
                await asyncio.to_thread(
                    reboot_cdata_onu, host=olt.host, port=olt.api_port or 22,
                    username=credential.username, encrypted_secret=credential.encrypted_secret,
                    pinned_host_key=pinned, pon_port=customer.olt_port, onu_id=customer.onu_id,
                )
                customer.wan_recovery_reboot_attempted_at = now
                await db.commit()
                await asyncio.sleep(20)
                online_after_reboot = await asyncio.to_thread(
                    verify_cdata_onu, host=olt.host, port=olt.api_port or 22,
                    username=credential.username, encrypted_secret=credential.encrypted_secret,
                    pinned_host_key=pinned, pon_port=customer.olt_port,
                    onu_id=customer.onu_id, serial_number=customer.ont_sn,
                )
                if not online_after_reboot:
                    return "waiting_session", "BestWeb sudah mereboot ONT; menunggu ONU kembali online untuk menyinkronkan WAN." + acs_note
                return "waiting_session", "BestWeb sudah menyinkronkan WAN dan mereboot ONT satu kali; menunggu PPPoE tersambung otomatis." + acs_note
            return "waiting_session", result.message + acs_note
        return "provisioned", result.message + acs_note
    except CDataExecutionError as exc:
        if psb_wan_sync_supported(customer) and "WAN ONU sudah ada tetapi tidak cocok" in str(exc):
            return "waiting_session", "ONU sudah terdaftar; WAN Internet akan diselaraskan dengan data pelanggan."
        return "failed", f"OLT gagal: {str(exc)[:300]}"
    except Exception:
        return "failed", "OLT gagal karena koneksi, credential, atau host-key tidak valid."


async def _run_onboarding_unlocked(customer: Customer, db, on_stage=None) -> OnboardingResult:
    """Run the complete supported path and persist only sanitized status text."""
    customer.provisioning_status = "running"
    customer.status = "configuring"
    olt_stage = "Mengonfigurasi dan memverifikasi ONT/OLT"
    await _notify(on_stage, olt_stage, 20, "processing", "Mengirim konfigurasi ke OLT dan memverifikasi ONT/WAN.")
    olt_status, olt_message = await _olt(customer, db, on_stage=on_stage)
    if olt_status in {"provisioned", "waiting_session"}:
        olt_state = "success" if olt_status == "provisioned" else "waiting"
        await _notify(on_stage, olt_stage, 50, olt_state, "Konfigurasi ONU tersimpan; memeriksa status WAN runtime." if olt_status == "waiting_session" else "Konfigurasi ONT/OLT terverifikasi.")
        mk_status, mk_message = await _mikrotik(customer, db, on_stage=on_stage)
    else:
        state = "failed" if olt_status == "failed" else "requires_action"
        await _notify(on_stage, olt_stage, 50, state, olt_message[:300])
        mk_status = "pending_configuration"
        mk_message = "Secret MikroTik belum dibuat karena konfigurasi ONU/WAN di OLT belum terverifikasi."
    customer.mikrotik_provisioning_status = mk_status
    customer.olt_provisioning_status = olt_status
    if mk_status == "provisioned" and olt_status == "provisioned":
        customer.provisioning_status = "provisioned"
        customer.status = "active"
        message = f"Provisioning live terverifikasi. {mk_message} {olt_message}"
    elif "failed" in (mk_status, olt_status):
        customer.provisioning_status = "failed"
        message = f"Provisioning belum selesai. {mk_message} {olt_message}"
    elif mk_status in {"waiting_session", "waiting_internet"} or olt_status == "waiting_session":
        customer.provisioning_status = "waiting_session" if olt_status == "waiting_session" else mk_status
        message = f"Menunggu koneksi pelanggan. {mk_message} {olt_message}"
    else:
        customer.provisioning_status = "pending_configuration"
        message = f"Menunggu konfigurasi perangkat. {mk_message} {olt_message}"
    customer.status = "active" if customer.provisioning_status == "provisioned" else customer.provisioning_status
    customer.provisioning_message = message
    db.add(ProvisioningEvent(
        customer_id=customer.id, target="onboarding", status=customer.provisioning_status,
        vendor=customer.olt_vendor, olt_device_id=customer.olt_device_id,
        mikrotik_device_id=customer.mikrotik_device_id,
        adapter_key="integrated-onboarding",
        payload_summary=json.dumps({"mikrotik": mk_status, "olt": olt_status}, sort_keys=True),
        message=message,
    ))
    await db.commit()
    return OnboardingResult(mk_status, olt_status, message)


async def run_onboarding(customer: Customer, db, on_stage=None) -> OnboardingResult:
    """Single hardware entry point; serialize duplicate clicks per customer."""
    lock = _ONBOARDING_LOCKS.setdefault(customer.id, asyncio.Lock())
    async with lock:
        return await _run_onboarding_unlocked(customer, db, on_stage=on_stage)


async def refresh_customer_online_status(customer: Customer, db) -> str:
    """Read-only reconciliation with explicit proof and an honest unknown state."""
    if (customer.ont_management_mode or "").strip().lower() == "acs_wifi_only":
        customer.status = "configured"; customer.provisioning_status = "configured"
        customer.olt_provisioning_status = "configured"; customer.mikrotik_provisioning_status = "not_applicable"
        customer.acs_wifi_status = "ready"
        customer.provisioning_message = "Configured: mode ACS WiFi-only; status ini bukan verifikasi Internet PPPoE."
        await db.commit()
        return "configured"
    from app.core.olt_vendor_profiles import get_vendor_profile
    olt_profile = get_vendor_profile(customer.olt_vendor, customer.olt_model)
    if not olt_profile or olt_profile.execution_state != "live_supported":
        state = "pending_configuration"
        message = f"PSB otomatis belum tersedia untuk OLT {customer.olt_vendor or '-'} {customer.olt_model or '-'}; adapter live belum tervalidasi."
        previous = (customer.provisioning_status, customer.mikrotik_provisioning_status, customer.olt_provisioning_status)
        customer.provisioning_status = state
        customer.status = state
        customer.mikrotik_provisioning_status = state
        customer.olt_provisioning_status = state
        customer.provisioning_message = message
        if previous != (state, state, state):
            db.add(ProvisioningEvent(
                customer_id=customer.id, target="live_reconciliation", status=state,
                vendor=customer.olt_vendor, olt_device_id=customer.olt_device_id,
                mikrotik_device_id=customer.mikrotik_device_id,
                adapter_key="unsupported-olt-blocked",
                payload_summary=json.dumps({"overall": state, "mikrotik": state, "olt": state}, sort_keys=True),
                message=message,
            ))
        await db.commit()
        return state
    olt_online: bool | None = None
    olt_wan_ready: bool | None = None
    mk_reachable = False
    mk_enabled: bool | None = None
    mk_active: bool | None = None
    internet_reachable: bool | None = None
    errors: list[str] = []

    olt = await db.get(OLTDevice, customer.olt_device_id)
    oc = await db.get(DeviceCredential, olt.credential_id) if olt and olt.credential_id else None
    pinned = os.getenv(f"OLT_{olt.id}_SSH_HOST_KEY_SHA256") if olt else None
    supported_cdata_models = {"FD1601S-B1", "FD1602S-B1"}
    if not (olt and olt.vendor == "C-DATA" and olt.model in supported_cdata_models):
        errors.append("Probe live OLT belum tersedia untuk vendor/model ini.")
    elif not (olt.is_active and oc and oc.is_active and pinned and olt.management_protocol == "ssh"):
        errors.append("Konfigurasi probe SSH OLT belum lengkap atau belum aktif.")
    else:
        verify_olt = verify_cdata_fd1602_onu if olt.model == "FD1602S-B1" else verify_cdata_onu
        olt_lock = _LIVE_OLT_PROBE_LOCKS.setdefault(olt.id, asyncio.Lock())
        async with olt_lock:
            try:
                olt_online = await asyncio.to_thread(
                    verify_olt, host=olt.host, port=olt.api_port or 22,
                    username=oc.username, encrypted_secret=oc.encrypted_secret,
                    pinned_host_key=pinned, pon_port=customer.olt_port,
                    onu_id=customer.onu_id, serial_number=customer.ont_sn,
                )
                if olt.model == "FD1602S-B1" and olt_online is False:
                    # Distinguish physical GPON registration from the FD1602
                    # runtime WAN gate. A disabled PPPoE WAN is not a
                    # physically offline ONU.
                    physical_online = await asyncio.to_thread(
                        verify_olt, host=olt.host, port=olt.api_port or 22,
                        username=oc.username, encrypted_secret=oc.encrypted_secret,
                        pinned_host_key=pinned, pon_port=customer.olt_port,
                        onu_id=customer.onu_id, serial_number=customer.ont_sn,
                        require_wan_runtime=False,
                    )
                    if physical_online:
                        olt_online = True
                        olt_wan_ready = False
                elif olt.model == "FD1602S-B1":
                    olt_wan_ready = True
            except Exception as exc:
                errors.append(f"Probe OLT gagal: {str(exc)[:160]}")
            try:
                rx_power, tx_power = await asyncio.to_thread(
                    read_cdata_ont_optical_power, host=olt.host, port=olt.api_port or 22,
                    username=oc.username, encrypted_secret=oc.encrypted_secret,
                    pinned_host_key=pinned, pon_port=customer.olt_port, onu_id=customer.onu_id,
                )
                if rx_power is not None:
                    customer.ont_rx_power = rx_power
                if tx_power is not None:
                    customer.ont_tx_power = tx_power
            except Exception as exc:
                _customer_log.warning("optical power read failed for customer id %s: %s", customer.id, str(exc)[:160])

    mk = await db.get(MikrotikDevice, customer.mikrotik_device_id)
    mc = await db.get(DeviceCredential, mk.credential_id) if mk and mk.credential_id else None
    if not (mk and mc):
        errors.append("Konfigurasi probe MikroTik belum lengkap.")
    else:
        try:
            adapter = RealMikrotikAdapter(
                host=mk.host, username=mc.username,
                password=decrypt_secret(mc.encrypted_secret), api_port=mk.api_port or 8728,
            )
            mk_enabled, mk_active, internet_reachable = await asyncio.to_thread(
                adapter.verify_pppoe_internet, customer.pppoe_username
            )
            mk_reachable = True
        except Exception as exc:
            errors.append(f"Probe MikroTik gagal: {str(exc)[:160]}")

    state, message = classify_live_connectivity(
        olt_online=olt_online, mikrotik_reachable=mk_reachable,
        secret_enabled=mk_enabled, pppoe_active=mk_active,
        internet_reachable=internet_reachable, errors=errors,
    )
    previous = (
        customer.provisioning_status,
        customer.mikrotik_provisioning_status,
        customer.olt_provisioning_status,
    )
    # A transport failure is not evidence that an already-online customer has
    # gone offline.  Preserve the last healthy state until a probe positively
    # reports an outage; this prevents green/yellow status flapping.
    preserve_last_healthy_state = (
        state == "unknown"
        and bool(errors)
        and previous == ("provisioned", "provisioned", "provisioned")
    )
    if preserve_last_healthy_state:
        state = "active"
        message = ("Probe perangkat sementara tidak tersedia; status aktif terakhir dipertahankan "
                   "sampai ada bukti gangguan nyata. Pemeriksaan akan diulang otomatis.")

    # Reconcile each stage from live device evidence. An unavailable probe is
    # unknown; a successful probe that reports the device offline is offline.
    # This monitor stays read-only so it never repeats an ONU reboot.
    if olt_online is True:
        customer.olt_provisioning_status = (
            "waiting_session" if olt_wan_ready is False else "provisioned"
        )
    elif olt_online is False:
        customer.olt_provisioning_status = "offline"
    elif errors and not preserve_last_healthy_state:
        customer.olt_provisioning_status = "unknown"

    if mk_enabled is True and mk_active is True:
        customer.mikrotik_provisioning_status = "provisioned" if state == "active" else "waiting_internet"
    elif mk_enabled is True:
        customer.mikrotik_provisioning_status = "waiting_session"
    elif mk_enabled is False:
        customer.mikrotik_provisioning_status = "failed"
    elif errors and not preserve_last_healthy_state:
        customer.mikrotik_provisioning_status = "unknown"

    # Never retain a commercial active flag when live customer connectivity
    # evidence is absent; status follows the exact session probe.
    customer.status = "active" if state == "active" else state
    customer.provisioning_status = "provisioned" if state == "active" else state
    customer.provisioning_message = message
    current = (
        customer.provisioning_status,
        customer.mikrotik_provisioning_status,
        customer.olt_provisioning_status,
    )
    if current != previous:
        db.add(ProvisioningEvent(
            customer_id=customer.id,
            target="live_reconciliation",
            status=customer.provisioning_status,
            vendor=customer.olt_vendor,
            olt_device_id=customer.olt_device_id,
            mikrotik_device_id=customer.mikrotik_device_id,
            adapter_key="live-device-monitor",
            payload_summary=json.dumps({
                "overall": customer.provisioning_status,
                "mikrotik": customer.mikrotik_provisioning_status,
                "olt": customer.olt_provisioning_status,
            }, sort_keys=True),
            message=message,
        ))
    await db.commit()
    return state
