import os
from pathlib import Path
from typing import Dict, Optional, Set

from defence360agent.subsys.panels.base import PanelException
from defence360agent.subsys.panels.no_cp import NoCP
from im360.subsys.panels.base import (
    ModSecurityInterface,
    PanelInterface,
    RemoteIPInterface,
)


class NoCPPanelInterface(PanelInterface):
    async def _get_all_admin_emails(self):
        return []

    def basedirs(self) -> Set[str]:
        return set()

    def http_ports(self) -> Set[int]:
        return set()

    def https_ports(self) -> Set[int]:
        return set()

    def remoteip_supported(self) -> bool:
        return True

    async def list_docroots(self) -> dict[str, str]:
        return {}


class NoCPModSecurityInterface(ModSecurityInterface):
    REBUILD_HTTPDCONF_CMD = None

    @classmethod
    async def installed_modsec(cls):
        return False

    async def _install_settings(self, reload_wafd=True):
        pass

    async def modsec_get_directive(self, directive_name, default=None):
        raise NotImplementedError

    async def reset_modsec_directives(self):
        pass

    async def reset_modsec_rulesets(self):
        pass

    async def revert_settings(self, reload_wafd=True):
        pass

    @classmethod
    def _get_conf_dir(cls):
        pass

    @classmethod
    def get_app_specific_waf_config(cls):
        raise NotImplementedError

    @classmethod
    def detect_cwaf(cls):
        return False

    @classmethod
    async def modsec_vendor_list(cls) -> list:
        """Return a list of installed ModSecurity vendors."""
        return []

    @classmethod
    async def enabled_modsec_vendor_list(cls) -> list:
        """Return a list of enabled ModSecurity vendors."""
        return []

    @classmethod
    async def build_vendor_file_path(cls, vendor: str, filename: str) -> Path:
        raise PanelException("not implemented")

    @classmethod
    async def get_i360_vendor_name(cls) -> str:
        raise PanelException("not implemented")

    @classmethod
    async def get_i360_vendor_version(cls) -> str:
        raise PanelException("not implemented")

    @classmethod
    async def invalidate_installed_vendors_cache(cls):
        pass

    @classmethod
    async def _apply_modsec_files_update(cls):
        pass

    @classmethod
    def get_audit_log_path(cls):
        # IMUNIFY360_NOCP_MODSEC_AUDIT_LOG should not be set generally,
        # used for tests only
        return os.environ.get("IMUNIFY360_NOCP_MODSEC_AUDIT_LOG", None)

    @classmethod
    def get_audit_logdir_path(cls):
        pass

    @classmethod
    def write_global_disabled_rules(cls, rule_list) -> bool:
        return False

    @classmethod
    async def sync_global_disabled_rules(cls, rule_list) -> bool:
        return False

    @classmethod
    async def sync_disabled_rules_for_domains(
        cls, domain_rules_map: Dict[str, list]
    ):
        pass


class NoCPRemoteIPInterface(RemoteIPInterface):
    async def remoteip_activated(self) -> bool:
        return True

    async def remoteip_install(self) -> Optional[str]:
        raise PanelException("not supported")


class NoControlPanel(
    NoCPModSecurityInterface,
    NoCPPanelInterface,
    NoCPRemoteIPInterface,
    NoCP,
):
    pure_ftp_conf_cls = None
