import errno
import getpass
import logging
import logging.config
import logging.handlers
import os
import stat
import struct
import sys
import time
import traceback
import uuid
from contextlib import contextmanager, suppress
from functools import lru_cache

import sentry_sdk
import yaml
from defence360agent.contracts import config, sentry
from defence360agent.contracts.config import AcronisBackup
from defence360agent.contracts.config import Logger as Config
from defence360agent.utils import antivirus_mode, is_root_user
from defence360agent.application import tags
from defence360agent.sentry import error_reporting_enabled
from defence360agent.sentry_filter import init_sentry
from defence360agent.sentry_routing import transport_options

PREFIX = os.environ.get("IMUNIFY360_LOGGING_PREFIX", "")
logger = logging.getLogger(__name__)

# update_error_reporting() turns it off in a running daemon
_error_reporting_on = True


def _drop_while_reporting_off(event, hint):
    return event if _error_reporting_on else None


def _sentry_init(debug=False):
    # Not Sentry.ENABLE: it is the schema default in a process that has
    # not rendered the merged config, such as the CLI or a starting daemon.
    if error_reporting_enabled():
        # The default LoggingIntegration sends every ERROR+ record, so a
        # SentryHandler on the root logger would send each one twice.
        init_sentry(
            **transport_options(),
            debug=debug,
            release=config.Core.VERSION,
            attach_stacktrace="on",
            before_send=_drop_while_reporting_off,
            # or the records logged meanwhile ride along with the next event
            before_breadcrumb=_drop_while_reporting_off,
        )
        with sentry_sdk.configure_scope() as scope:
            for tag, value in sentry.tags().items():
                scope.set_tag(tag, value)
            scope.user = {"id": sentry.tag("server_id")}


def update_error_reporting():
    """Apply a changed ERROR_REPORTING.enable to the running process.

    It only stops and resumes the events: a process that started with
    reporting off starts Sentry on its next restart.
    """
    global _error_reporting_on
    _error_reporting_on = error_reporting_enabled()


class _LoggerDynConfig:
    _ROOT_LOG_DIR = "/var/log/%s" % config.Core.PRODUCT

    @staticmethod
    def _user_log_dir():
        return "/var/log/%s_user_logs/%s" % (
            config.Core.PRODUCT,
            getpass.getuser() or os.getuid(),
        )

    def __init__(self):
        is_root = is_root_user()
        self.log_dir = self._ROOT_LOG_DIR if is_root else self._user_log_dir()
        _sentry_init()

        self.mutableDictConfig = {
            "loggers": {
                "network": {
                    "level": "DEBUG",
                    # network_log is disabled by default'
                    "handlers": [],
                },
                "defence360agent.internals.the_sink": {
                    "level": "DEBUG",
                    # process_message_log is disabled by default'
                    "handlers": [],
                },
                "event_hook": {
                    "level": "INFO",
                    "handlers": [],
                },
            },
            "version": 1,
            "handlers": {
                "error_log": {
                    "level": "WARNING",
                    "formatter": "abstimestamp",
                    "filename": "%s/error.log" % self.log_dir,
                    "class": "logging.FileHandler",
                    "encoding": "utf8",
                },
                "network_log": {
                    "level": "DEBUG",
                    "formatter": "abstimestamp",
                    "filename": "%s/network.log" % self.log_dir,
                    "class": "logging.FileHandler",
                    "encoding": "utf8",
                },
                "debug_log": {
                    "level": "DEBUG",
                    "formatter": "abstimestamp",
                    "filename": "%s/debug.log" % self.log_dir,
                    "class": "logging.FileHandler",
                    "encoding": "utf8",
                },
                "console_log": {
                    "level": "INFO",
                    "formatter": "abstimestamp",
                    "filename": "%s/console.log" % self.log_dir,
                    "class": "logging.FileHandler",
                    "encoding": "utf8",
                },
                "hook_log": {
                    "level": "INFO",
                    "formatter": "eventhook",
                    "filename": "%s/hook.log" % self.log_dir,
                    "class": "logging.FileHandler",
                    "encoding": "utf8",
                },
                "console": {
                    "formatter": "abstimestamp",
                    "class": "logging.StreamHandler",
                    "stream": "ext://sys.stderr",
                    "level": "INFO",
                },
                "process_message_log": {
                    "formatter": "reltimestamp",
                    # DEF-26794: append mode (default). With logrotate's
                    # copytruncate, mode="w" would leave the fd offset past
                    # EOF after truncation and re-inflate the file with
                    # sparse zeros. O_APPEND seeks to the (now-zero) end
                    # before each write, so the file size resets cleanly.
                    "level": "DEBUG",
                    "filename": "%s/process_message.log" % self.log_dir,
                    "class": "logging.FileHandler",
                    "encoding": "utf8",
                },
            },
            "root": {
                "level": "NOTSET",
                "handlers": [
                    "console_log",
                    # 'debug_log' is disabled by default,
                    "error_log",
                ],
            },
            "mkdir": "logs",
            "formatters": {
                "reltimestamp": {
                    "format": (
                        "%(levelname)-7s [+%(relativeCreated)5dms] "
                        f"{PREFIX}%(name)50s|%(message)s"
                    )
                },
                "abstimestamp": {
                    "format": (
                        f"%(levelname)-7s [%(asctime)s] {PREFIX}%(name)s:"
                        " %(message)s"
                    )
                },
                "eventhook": {"format": "%(created)d : %(message)s"},
            },
            "disable_existing_loggers": False,
        }

        self.mutableDictConfig["loggers"]["AcronisClientInstaller"] = {
            "level": "INFO",
            "handlers": [],
        }
        self.mutableDictConfig["handlers"]["acronis_installer_log"] = {
            "formatter": "abstimestamp",
            # DEF-26794: append mode (default). See process_message_log
            # comment above for why mode="w" is unsafe with copytruncate.
            "level": "INFO",
            "filename": os.path.join(self.log_dir, AcronisBackup.LOG_NAME),
            "class": "logging.FileHandler",
            "encoding": "utf8",
        }

        if not is_root:
            # The per-user log dir is owned by the unprivileged user, so root's
            # logrotate must not rotate it (it would let the user redirect
            # root's create/copy/truncate via a symlink). Bound these logs
            # in-process instead — as the owning user — mirroring the size
            # policy logrotate applies to the root logs.
            for handler in self.mutableDictConfig["handlers"].values():
                if handler.get("class") == "logging.FileHandler":
                    handler["class"] = "logging.handlers.RotatingFileHandler"
                    handler["maxBytes"] = Config.MAX_LOG_FILE_SIZE
                    handler["backupCount"] = Config.BACKUP_COUNT


@lru_cache(1)
def _late_init():
    return _LoggerDynConfig()


def _we_are_in_cagefs():
    """
    :return bool: True if python interpreter is being run in CageFS container,
        otherwise False
    :raise: never

    Current implementation simply checks "/var/.cagefs" presence, as
    Anton Volkov consulted us to do.

    Placing this function not in 'subsys' package, because 'logger' module
    is one of cornerstones dependency for 'subsys' package as well.
    """
    with suppress(OSError):
        return os.path.exists("/var/.cagefs")


_ACL_ACCESS = "system.posix_acl_access"
_ACL_DEFAULT = "system.posix_acl_default"
_ACL_XATTR_VERSION = 2
_ACL_HEADER = struct.Struct("<I")
_ACL_ENTRY = struct.Struct("<HHI")
_ACL_GROUP_OBJ = 0x04
_ACL_MASK = 0x10
_ACL_OTHER = 0x20
_ACL_NAMED_TAGS = (0x02, 0x08)  # ACL_USER, ACL_GROUP
# the default ACL entries that grant a new file's group and other bits;
# the mask also caps the named entries
_ACL_DEFAULT_CLAMPED = (_ACL_GROUP_OBJ, _ACL_MASK, _ACL_OTHER)
_ACL_READ_TRAVERSE = 0o5
_ACL_WRITE = 0o2
# the path has no such ACL, or its filesystem has no ACLs
_ACL_ABSENT = (errno.ENODATA, errno.EOPNOTSUPP)


def _acl_entries(path, xattr_name):
    """Return the ACL stored in path's xattr_name attribute as a list of
    (tag, perm, id) entries, in their stored order.

    [] when there is no ACL; None when it can't be read or is malformed.
    The xattr layout is the kernel's posix_acl_xattr (little-endian
    version header, then tag/perm/id entries).
    """
    try:
        blob = os.getxattr(path, xattr_name)
    except OSError as e:
        return [] if e.errno in _ACL_ABSENT else None
    entries = blob[_ACL_HEADER.size :]
    if (
        len(blob) < _ACL_HEADER.size
        or len(entries) % _ACL_ENTRY.size
        or _ACL_HEADER.unpack_from(blob)[0] != _ACL_XATTR_VERSION
    ):
        return None
    return list(_ACL_ENTRY.iter_unpack(entries))


def _acl_perms(path):
    """Return the rwx bits of path's access ACL as a pair: the union of its
    named user and group entries, and its owning group entry.

    (0, 0) when there is no ACL or it can't be read.
    """
    named = owning_group = 0
    for tag, perm, _ in _acl_entries(path, _ACL_ACCESS) or ():
        if tag in _ACL_NAMED_TAGS:
            named |= perm
        elif tag == _ACL_GROUP_OBJ:
            owning_group |= perm
    return named & 0o7, owning_group & 0o7


@contextmanager
def _warn_on_failure(failure, path, warned=OSError):
    try:
        yield
    except FileNotFoundError:
        # removed during the walk by a log rotation, or a dangling symlink,
        # so there is nothing left to change
        pass
    except warned as e:
        sys.stderr.write("[WARNING] {} {}: {}\n".format(failure, path, e))


def _drop_default_acl_write(path):
    """Clear write from the owning group, mask and other entries of path's
    default ACL, so that only the owner gets write on a new file.

    Return whether path is left with a default ACL that grants no one else
    write, or none: False when it can't be read, is malformed, or can't be
    written. The ACL is not rewritten when it already grants no write.
    """
    entries = _acl_entries(path, _ACL_DEFAULT)
    if entries is None:
        return False
    clamped = [
        (
            tag,
            perm & ~_ACL_WRITE if tag in _ACL_DEFAULT_CLAMPED else perm,
            qualifier,
        )
        for tag, perm, qualifier in entries
    ]
    if clamped == entries:
        return True
    value = _ACL_HEADER.pack(_ACL_XATTR_VERSION) + b"".join(
        _ACL_ENTRY.pack(*entry) for entry in clamped
    )
    with _warn_on_failure("cannot drop write from the default ACL of", path):
        os.setxattr(path, _ACL_DEFAULT, value)
        return True
    return False


def _chmod_log_dirs(dirname, dir_perm, file_perm, keep_acl_readers=False):
    """Change file/dir modes recursively.

    Starting at dirname, change all inner directory permissions to dir_perm,
    file permissions to file_perm. The walk is top-down, so a directory is
    shut, when it has any bit beyond its new mode, before the files below
    it may regain group write. A directory that opens to its ACL readers
    gets its new mode only after the walk has chmodded every file, the
    deepest directory first, and stays at dir_perm if the walk could not
    list it or chmod one of its entries, including one it could not shut
    at the end. The files of a directory it could not shut get no group
    write. Its default ACL is clamped (see below) before the walk lists
    its contents.

    With keep_acl_readers, a directory whose access ACL has named entries
    gets group bits, which chmod stores as the ACL mask, that let those
    entries read and traverse but not write: the read and traverse bits the
    entries ask for, minus any its owning group entry also has (the mask
    would open them to the owning group too). Every file loses group write,
    whatever its own ACL: the directories keep their owning group out, so
    group write could only serve a group named on a directory that also
    owns the file.
    A directory's default ACL loses write in all but its owner entry: the
    kernel gives a new file or subdirectory those entries, cut only by its
    creation mode, so without this a log created between two passes would
    let the named entries write it, or let a reader write it as its owning
    group or as other. A directory whose default ACL can't be read, parsed
    or written stays at dir_perm for the same reason.
    A symlinked directory below dirname gets plain dir_perm on its target,
    because os.walk does not descend into it to clamp its contents; if the
    target is in the tree, the walk opens it again under its own name.

    A path that vanishes during the walk is skipped. Permission errors,
    and with keep_acl_readers any failure to write a default ACL, are
    logged to stderr and ignored; other chmod errors are raised.
    """

    def _os_chmod(file_dir_path, permission):
        """Return False when a refused chmod left the old mode."""
        with _warn_on_failure(
            "cannot chmod on", file_dir_path, PermissionError
        ):
            with suppress(FileNotFoundError):
                os.chmod(file_dir_path, permission)
            return True
        return False

    opening = []
    # the directories the walk could not list or chmod an entry of
    kept_shut = set()
    # the directories it could not shut, whose files keep no group write
    left_open = set()

    def _chmod_dir(dir_path, parent=None):
        permission = dir_perm
        # the default ACL first: the readers must not reach the directory
        # while its default ACL would let them write the logs created there
        if keep_acl_readers and _drop_default_acl_write(dir_path):
            named, owning_group = _acl_perms(dir_path)
            kept = named & ~owning_group & _ACL_READ_TRAVERSE
            permission |= kept << 3
        shut = True
        if permission == dir_perm or _wider_than(dir_path, permission):
            shut = _os_chmod(dir_path, dir_perm)
        if permission != dir_perm:
            opening.append((dir_path, permission, parent))
        return shut

    def _wider_than(dir_path, permission):
        try:
            return stat.S_IMODE(os.stat(dir_path).st_mode) & ~permission
        except FileNotFoundError:
            return False

    if keep_acl_readers:
        file_perm &= ~stat.S_IWGRP
    # os.walk lists a directory only after the loop body has handled it
    # as an entry of its parent
    if not _chmod_dir(dirname):
        left_open.add(dirname)
    for path, dirs, files in os.walk(
        dirname, onerror=lambda e: kept_shut.add(e.filename)
    ):
        for directory in dirs:
            inner = os.path.join(path, directory)
            if keep_acl_readers and os.path.islink(inner):
                chmodded = _os_chmod(inner, dir_perm)
            else:
                chmodded = _chmod_dir(inner, path)
                if not chmodded:
                    left_open.add(inner)
            if not chmodded:
                kept_shut.add(path)
        permission = file_perm
        if path in left_open:
            permission &= ~stat.S_IWGRP
        for name in files:
            if not _os_chmod(os.path.join(path, name), permission):
                kept_shut.add(path)
    for dir_path, permission, parent in reversed(opening):
        # an entry of the directory may have kept a mode that lets the
        # readers write it
        shutting = dir_path in kept_shut
        if shutting:
            permission = dir_perm
        # the deepest first, so a refused shut reaches the parent in time
        if not _os_chmod(dir_path, permission) and shutting:
            kept_shut.add(parent)


def _keeps_acl_readers(log_dir):
    """Whether the admin opted the main log tree in to keeping ACL readers.

    Never for the per-user log dirs; an unreadable opt-in means no.
    """
    if log_dir != _LoggerDynConfig._ROOT_LOG_DIR:
        return False
    try:
        return bool(Config.KEEP_ACL_READERS)
    except (OSError, ValueError):
        return False


def reconfigure():
    """
    Re-catch with _LoggerDynConfig and re-open log files
    """
    if os.getenv("IMUNIFY360_DISABLE_LOGGING"):
        pass
    else:
        try:
            # Set sentry.TAGS from saved file
            tags.cached_fill()
            log_dir = _late_init().log_dir
            os.makedirs(log_dir, Config.LOG_DIR_PERM, exist_ok=True)
            _chmod_log_dirs(
                log_dir,
                Config.LOG_DIR_PERM,
                Config.LOG_FILE_PERM,
                keep_acl_readers=_keeps_acl_readers(log_dir),
            )
            logging.config.dictConfig(_late_init().mutableDictConfig)
        except OSError:
            # We do not create user logs to keep user isolation
            # level high.
            #
            # Another alternative is
            # cagefs.mp:%/var/log/imunify360_user_log
            # but it is not working for some reason, we need to find out
            # later why.

            if not _we_are_in_cagefs():
                traceback.print_exc(file=sys.stderr)
                sys.stderr.write(
                    "%s logger is not available.\n" % config.Core.PRODUCT
                )
        except Exception:
            # be robust: do not die if dictConfig fails
            traceback.print_exc(file=sys.stderr)
            sys.stderr.write(
                "%s logger is not available.\n" % config.Core.PRODUCT
            )
        else:  # logging is configured successfully
            sys.excepthook = _log_uncaught_exceptions


def _log_uncaught_exceptions(exc_type, exc_value, exc_traceback):
    if issubclass(exc_type, KeyboardInterrupt):
        sys.__excepthook__(exc_type, exc_value, exc_traceback)
        return

    logger.critical(
        "uncaught exception", exc_info=(exc_type, exc_value, exc_traceback)
    )


def update_logging_config_from_file(filename):
    with open(filename) as config_file:
        config = yaml.safe_load(config_file)
    _late_init().mutableDictConfig.update(config)

    reconfigure()


def get_fds():
    handlers = logging.root.handlers
    for _logger in _late_init().mutableDictConfig["loggers"].keys():
        handlers.extend(logging.getLogger(_logger).handlers)

    return [
        h.stream
        for h in handlers
        if hasattr(h, "stream")
        and hasattr(h.stream, "fileno")
        and h.stream != sys.stderr
    ]


def get_log_file_names():
    return [
        values["filename"]
        for _, values in _late_init().mutableDictConfig["handlers"].items()
        if "filename" in values
    ]


def getNetworkLogger(name):
    if name in sys.modules:
        return logging.getLogger("network." + sys.modules[name].__name__)
    else:
        return logging.getLogger("network." + name)


# NOTE: client expects that this function will return
# the same value always - /var/log/imunify360. They base their logrotate
# configs on this value. In case of some updates, corresponding teams
# should be notified before update to update their logrotate configs.
def log_dir() -> str:
    """
    Return base log directory for the product.
    Supposed to be used by clients to build the path to their own logs.
    """
    return _late_init().log_dir


def setLogLevel(verbose):
    # FIXME
    if antivirus_mode.disabled:
        _late_init().mutableDictConfig["loggers"]["AcronisClientInstaller"][
            "handlers"
        ].append("acronis_installer_log")
    if verbose >= 2:
        _late_init().mutableDictConfig["loggers"]["network"][
            "handlers"
        ].append("network_log")
    if verbose >= 3:
        _late_init().mutableDictConfig["loggers"][
            "defence360agent.internals.the_sink"
        ]["handlers"].append("process_message_log")
    if verbose >= 4:
        _late_init().mutableDictConfig["root"]["handlers"].append("debug_log")
    _late_init().mutableDictConfig["loggers"]["event_hook"]["handlers"].append(
        "hook_log"
    )

    reconfigure()


def setConsoleLogLevel(newloglevel):
    """
    also results in reconfigure()
    """
    _late_init().mutableDictConfig["handlers"]["console"][
        "level"
    ] = newloglevel
    reconfigure()


# openAibolitActionsLog and openMdsActionsLog are deprecated and should be removed
# after release of https://gerrit.cloudlinux.com/c/defence360/+/225868
@contextmanager
def openAibolitActionsLog(scan_id: str):
    path = os.path.join(_late_init().log_dir, "aibolit_actions.log")
    with open(path, "a") as f:
        f.write(f'{time.strftime("%Y-%m-%d %H:%M:%S")} | {scan_id} | ')
        yield f
        f.write("\n\n")


# openAibolitActionsLog and openMdsActionsLog are deprecated and should be removed
# after release of https://gerrit.cloudlinux.com/c/defence360/+/225868
@contextmanager
def openMdsActionsLog(scan_id: str):
    log_dir = _late_init().log_dir
    os.makedirs(log_dir, exist_ok=True)
    path = os.path.join(log_dir, "mds_actions.log")
    with open(path, "a") as f:
        f.write(f'{time.strftime("%Y-%m-%d %H:%M:%S")} | {scan_id} | ')
        yield f
        f.write("\n\n")


class EventHookLogger:
    class _EventLogger:
        class _HookLogger:
            tpl = (
                "{uuid:s} : {action:s} {native:s}: "
                "{event:s} : {subtype:s} : {path:s}"
            )

            def __init__(self, parent, path, native):
                self.path = path
                self.event = parent.event
                self.subtype = parent.subtype
                self.uuid = parent.uuid
                self.log = parent.log
                self.native = native

            def __enter__(self):
                return self

            def __exit__(self, exc_type, exc_val, exc_tb):
                pass

            def _log(self, action, message=""):
                data = {
                    "uuid": str(self.uuid),
                    "action": action,
                    "native": "native " if self.native else "",
                    "event": self.event,
                    "subtype": self.subtype,
                    "path": self.path,
                }
                msg = self.tpl.format(**data)

                if message:
                    msg = " : ".join([msg, message])

                self.log(msg)

            def begin(self):
                self._log("started")

            def finish(self, exit_code, err):
                message = "OK" if exit_code == 0 else "ERROR"
                if exit_code:
                    message = ":".join([message, str(exit_code)])
                if err:
                    if isinstance(err, bytes):
                        err = err.decode(errors="backslashreplace")
                    message = "\n".join([message, err])

                self._log("done", message)

        def __init__(self, parent, event, subtype):
            self.event = event
            self.subtype = subtype
            self.uuid = uuid.uuid4()
            self.log = parent.log

        def __call__(self, path, native=False):
            return self._HookLogger(self, path, native=native)

        def __enter__(self):
            return self

        def __exit__(self, exc_type, exc_val, exc_tb):
            pass

    def __init__(self):
        logger = logging.getLogger("event_hook")
        self.log = logger.info

    def __call__(self, event, subtype):
        return self._EventLogger(self, event, subtype)
