"""
In-process helper functions to wrap IPC calls to the database worker.
"""

import logging
from multiprocessing import Queue
from typing import Any

logger = logging.getLogger(__name__)


def record_device(
    db_queue: "Queue[tuple[str, dict[str, Any]]]",
    mac_addr: str | None = None,
    ip_addr: str | None = None,
    dhcp_hostname: str | None = None,
) -> None:
    """
    Wrapper function to record a device in the database.
    Wraps the IPC call to the database worker.
    """
    db_queue.put(
        (
            "record_device",
            {
                "mac_addr": mac_addr,
                "ip_addr": ip_addr,
                "dhcp_hostname": dhcp_hostname,
            },
        )
    )


def write_pending_flows(
    db_queue: "Queue[tuple[str, dict[str, Any]]]",
    flow_dict: dict[str, Any],
    gateway_mac_addr: str,
) -> None:
    """
    Wrapper function to write pending flows to the database.
    Wraps the IPC call to the database worker.
    """
    db_queue.put(
        (
            "write_pending_flows",
            {
                "flow_dict": flow_dict,
                "gateway_mac_addr": gateway_mac_addr,
            },
        )
    )


def save_mdns_record(
    db_queue: "Queue[tuple[str, dict[str, Any]]]",
    ip4: str | None,
    ip6s: list[str],
    mdns_name: str | None,
    service: str,
    server: str | None,
    properties: dict[bytes, bytes | None],
    integration: str,
    integration_data: dict[str, Any],
) -> None:
    """
    Wrapper function to save an mDNS record in the database.
    Wraps the IPC call to the database worker.
    """
    db_queue.put(
        (
            "save_mdns_record",
            {
                "ip4": ip4,
                "ip6s": ip6s,
                "mdns_name": mdns_name,
                "service": service,
                "server": server,
                "properties": properties,
                "integration": integration,
                "integration_data": integration_data,
            },
        )
    )


def clear_pending_queue(db_queue: "Queue[tuple[str, dict[str, Any]]]") -> None:
    """
    Wrapper function to clear the pending queue.
    Wraps the IPC call to the database worker.
    """
    db_queue.put(("clear_pending_queue", {}))


def ssdp_save(
    db_queue: "Queue[tuple[str, dict[str, Any]]]",
    discovered_device_dict: dict[str, Any],
) -> None:
    """
    Wrapper function to save SSDP data in the database.
    Wraps the IPC call to the database worker.
    """
    db_queue.put(("ssdp_save", discovered_device_dict))


def update_device_metadata(db_queue: "Queue[tuple[str, dict[str, Any]]]") -> None:
    db_queue.put(("update_device_metadata", {}))
