"""Quick discovery: a small, fast, read-only snapshot.

Runs during install/registration so the panel has baseline facts immediately.
Intentionally cheap — no subprocess-heavy scans here.
"""

from __future__ import annotations

from typing import Any

from discovery import system, services
from logger import get_logger

log = get_logger("discovery.quick")


def collect() -> dict[str, Any]:
    """Return the minimal baseline server facts."""
    return {
        "hostname": system.hostname(),
        "os": system.os_info(),
        "kernel": system.kernel(),
        "architecture": system.architecture(),
        "cpu_count": system.cpu_count(),
        "memory_total": system.memory_total_bytes(),
        "disk_total": system.disk_total_bytes(),
        "public_ip": system.public_ip(),  # placeholder for v1
        "private_ips": system.private_ips(),
        "uptime": system.uptime_seconds(),
        "certbot_available": services.certbot_available(),
    }


def run(config, api_client, upload: bool = True) -> dict[str, Any]:
    """Collect baseline facts and upload them as an instant server_facts scan.

    The quick scan gives the panel non-empty server_facts immediately after the
    first connection, ahead of the slower full inventory. Upload is best-effort.
    """
    snapshot = collect()
    if upload:
        from discovery.upload import upload_inventory

        try:
            upload_inventory(config, api_client, "quick", {"server_facts": [snapshot]})
        except Exception as exc:  # noqa: BLE001 - upload is best-effort
            log.warning("quick scan upload failed: %s", exc)
    return snapshot
