import json
from abc import ABC, abstractmethod
from typing import List

import psutil
from ambient_backend_api_client.models import InterfaceTypeEnum, NetworkInterface
from result import Ok

from ambient_edge_server.utils import logger


class InterfaceService(ABC):
    @abstractmethod
    async def get_network_interfaces(self) -> List[NetworkInterface]:
        """Get network interfaces on the device."""


class LinuxInterfaceService(InterfaceService):
    def __init__(self) -> None:
        self._running = False
        self._error = Ok("initialized")

    async def get_network_interfaces(self) -> List[NetworkInterface]:
        logger.info("Starting to fetch network interface details.")
        interfaces = psutil.net_if_addrs()
        logger.debug("Interfaces: %s", json.dumps(interfaces, indent=4))
        network_interfaces = []

        for interface_name, addresses in interfaces.items():
            logger.debug("Processing %s interface ...", interface_name)
            logger.debug("Addresses: %s", addresses)

            for addr in addresses:
                details = {
                    "name": f"{interface_name}-{addr.address}",
                    # Default to OTHER, adjust logic as needed
                    "type": InterfaceTypeEnum.UNKNOWN,
                }
                logger.debug("Address: %s", addr)
                if addr.family == 2:
                    details["ipv4_address"] = addr.address
                    details["netmask"] = addr.netmask
                elif addr.family == 10:
                    details["ipv6_address"] = addr.address
                details["broadcast"] = addr.broadcast

                # Interface type heuristic
                wifi_heuristics = ["wi-fi", "wifi", "wlan"]
                ethernet_heuristics = [
                    "eth",
                    "enp",
                    "ens",
                    "eno",
                    "enx",
                    "docker",
                    "lo",
                    "br",
                ]

                if any(
                    heuristic in interface_name.lower() for heuristic in wifi_heuristics
                ):
                    details["type"] = InterfaceTypeEnum.WIFI
                elif any(
                    heuristic in interface_name.lower()
                    for heuristic in ethernet_heuristics
                ):
                    details["type"] = InterfaceTypeEnum.ETHERNET

                network_interface = NetworkInterface(**details)
                logger.debug(
                    "Network Interface: %s", network_interface.model_dump_json(indent=4)
                )
                network_interfaces.append(network_interface)
            logger.info(f"Added {interface_name} to network interfaces.")

        logger.info("Completed fetching all network interfaces.")
        return network_interfaces
