"""
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.


This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.


You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.

Copyright © 2019 Cloud Linux Software Inc.

This software is also available under ImunifyAV commercial license,
see <https://www.imunify360.com/legal/eula>
"""
import os
from copy import deepcopy
from typing import List, Optional, Set

from playhouse.shortcuts import model_to_dict

from defence360agent.contracts.messages import (
    Accumulatable,
    Ack,
    FilesUpdated,
    Lockable,
    Message,
    Noop,
    Received,
    estimate_size,
    Reportable,
    ShortenReprListMixin,
    Splittable,
)
from defence360agent.model import Model
from imav.malwarelib.cleanup.reasons import (
    failure_for_server,
    result_for_server,
)
from imav.malwarelib.config import QueuedScanState
from imav.malwarelib.scan.mds.report import (
    MalwareDatabaseAppInfo,
    MalwareDatabaseHitInfo,
    MalwareDatabaseScanReport,
    SCAN_FAILED,
)
from imav.malwarelib.scan.utils import SCAN_LOG_MAX_LENGTH, trim


class MalwareScanTask(Message):
    """
    Creates task to scan file for malware
    """

    DEFAULT_METHOD = "MALWARE_SCAN_TASK"


class MalwareScanComplete(Message):
    DEFAULT_METHOD = "MALWARE_SCAN_COMPLETE"


class MalwareCleanComplete(Message):
    DEFAULT_METHOD = "MALWARE_CLEAN_COMPLETE"


class MalwareRestoreComplete(Message):
    DEFAULT_METHOD = "MALWARE_RESTORE_COMPLETE"


# Upper bound on the number of entries kept in the unbounded summary error
# array. A summary carries no per-file results, so the errors and the captured
# scanner output are the only things that can inflate it past the transport
# payload limit on huge scans.
MALWARE_SUMMARY_MAX_ARRAY = int(
    os.environ.get("IMUNIFY360_MALWARE_SUMMARY_MAX_ARRAY", 1000)
)


class MalwareScanSummary(
    ShortenReprListMixin, Message, Reportable, Splittable
):
    DEFAULT_METHOD = "MALWARE_SCAN_SUMMARY"

    def __init__(self, *args, items, **kwargs):
        # keep the summary only without results
        items = [{"summary": self._trim(item["summary"])} for item in items]
        super().__init__(*args, items=items, **kwargs)

    @classmethod
    def _message_size(cls, message) -> int:
        # the results are dropped in __init__, so charging the budget for
        # them would split scans that send only a few kilobytes of summary
        return estimate_size(
            {"summary": cls._trim(message.get("summary") or {})}
        )

    @staticmethod
    def _trim(summary):
        trimmed = None

        def store(field, value, original_length):
            nonlocal trimmed
            if trimmed is None:
                trimmed = dict(summary)
            trimmed[field] = value
            trimmed[f"{field}_truncated"] = original_length

        value = summary.get("errors")
        if isinstance(value, list) and len(value) > MALWARE_SUMMARY_MAX_ARRAY:
            store("errors", value[:MALWARE_SUMMARY_MAX_ARRAY], len(value))
        for field in ("stdout", "stderr"):
            value = summary.get(field)
            if isinstance(value, str) and len(value) > SCAN_LOG_MAX_LENGTH:
                store(
                    field,
                    trim(value, max_length=SCAN_LOG_MAX_LENGTH),
                    len(value),
                )
        return trimmed if trimmed is not None else summary


#: Longest argv entry reproduced verbatim in a result message's `args`.
#: `args` is copied into every result batch of a scan (Splittable rebuilds
#: the summary per batch), so one oversized entry is paid for once per
#: thousand hits -- the malware Ignore List serialised into
#: --ignore-filenames reaches tens of kilobytes on a busy host, see
#: DEF-52663. Anything longer is replaced by a marker stating its size; the
#: verbatim command stays in the MALWARE_SCAN_SUMMARY message, which is sent
#: once per scan and carries the same scanid.
MALWARE_RESULT_MAX_ARG = int(
    os.environ.get("IMUNIFY360_MALWARE_RESULT_MAX_ARG", 1024)
)


class MalwareScanResult(ShortenReprListMixin, Message, Reportable, Splittable):
    DEFAULT_METHOD = "MALWARE_SCAN_RESULT"

    LIST_SIZE = 5

    BATCH_SIZE = 1000
    BATCH_FIELD = "results"

    def __init__(self, *args, items, **kwargs):
        items = [
            {
                # keep summary fields necessary for storing results only
                "summary": {
                    key: self._elide_long_args(value)
                    if key == "args"
                    else value
                    for key, value in item["summary"].items()
                    if key in {"scanid", "type", "args"}
                },
                "results": self._public_results(item["results"]),
            }
            for item in items
        ]
        super().__init__(*args, items=items, **kwargs)

    @staticmethod
    def _public_results(results):
        # Older producers/retained messages can carry lists or list-valued rows.
        # Only the aggregated file-hit shape carries our private offsets.
        if not isinstance(results, dict):
            return results
        public = dict(results)
        for path, data in results.items():
            if not isinstance(data, dict) or not isinstance(
                data.get("hits"), list
            ):
                continue
            if any(
                isinstance(hit, dict) and "_match_offset" in hit
                for hit in data["hits"]
            ):
                public[path] = {
                    **data,
                    "hits": [
                        {
                            key: value
                            for key, value in hit.items()
                            if key != "_match_offset"
                        }
                        if isinstance(hit, dict)
                        else hit
                        for hit in data["hits"]
                    ],
                }
        return public

    @staticmethod
    def _elide_long_args(args):
        """Replace oversized argv entries with a marker of their size."""
        if not isinstance(args, (list, tuple)):
            return args
        return [
            MalwareScanResult._elide(arg) if isinstance(arg, str) else arg
            for arg in args
        ]

    @staticmethod
    def _elide(arg):
        if len(arg) <= MALWARE_RESULT_MAX_ARG:
            return arg
        marker = "<{} bytes elided, see MALWARE_SCAN_SUMMARY>".format(len(arg))
        # never grow the payload (possible only at a tiny configured limit)
        return marker if len(marker) < len(arg) else arg


class MalwareScan(Accumulatable, Lockable):
    """
    Represents results of single scan
    """

    DEFAULT_METHOD = "MALWARE_SCAN"
    LIST_CLASS = (MalwareScanSummary, MalwareScanResult)

    def do_accumulate(self) -> bool:
        results = self.get("results")
        return results is not None


class MalwareMRSUpload(Message):
    """
    Used to isolate a possibly long running uploading to MRS from the
    other MalwareScan handlers.
    """

    DEFAULT_METHOD = "MALWARE_MRS_UPLOAD"


class MalwareScanQueue(Message):
    DEFAULT_METHOD = "MALWARE_SCAN_QUEUE"


class MalwareScanQueuePut(MalwareScanQueue):
    def __init__(self, *, paths, scan_args):
        super().__init__(paths=paths, scan_args=scan_args)


class MalwareScanQueueRemove(MalwareScanQueue):
    def __init__(self, *, scan_ids):
        super().__init__(scan_ids=scan_ids)


class MalwareScanQueueRecheck(MalwareScanQueue):
    def __init__(self):
        super().__init__()


class MalwareScanQueueStopBackground(MalwareScanQueue):
    def __init__(self):
        super().__init__()


class MalwareScanQueueUpdateStatus(MalwareScanQueue):
    def __init__(
        self, *, scan_ids: List[str] = None, status: QueuedScanState = None
    ):
        super().__init__(scan_ids=scan_ids, status=status)


class MalwareResponse(Message, Received):
    TYPES = BLACK, WHITE, KNOWN, UNKNOWN = "BLACK", "WHITE", "KNOWN", "UNKNOWN"
    DEFAULT_METHOD = "MALWARE_RESPONSE"


class MalwareSendFiles(Message, Received):
    DEFAULT_METHOD = "MALWARE_SEND_FILES"


class MalwareRescanFiles(Message, Received):
    DEFAULT_METHOD = "MALWARE_RESCAN_FILES"


class MalwareCleanupTask(Message):
    """
    Creates task to cleanup files
    """

    DEFAULT_METHOD = "MALWARE_CLEANUP_TASK"

    def __init__(
        self,
        *,
        hits,
        standard_only=None,
        cause=None,
        initiator=None,
        scan_id=None,
        post_action=None,
    ):
        super().__init__(
            hits=hits,
            standard_only=standard_only,
            cause=cause,
            initiator=initiator,
            scan_id=scan_id,
            post_action=post_action,
        )


class MalwareCleanupList(
    ShortenReprListMixin, Message, Reportable, Splittable
):
    DEFAULT_METHOD = "MALWARE_CLEANUP_LIST"

    LIST_SIZE = 5
    BATCH_SIZE = 1000
    BATCH_FIELD = "hits"
    #: hit columns the agent keeps for itself (the stop step's file identity)
    LOCAL_HIT_FIELDS = ("file_dev", "file_ino", "file_uid")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if isinstance(self.get("items"), list):
            self["items"] = [self._for_server(item) for item in self["items"]]

    @classmethod
    def _for_server(cls, item):
        if not isinstance(item, dict):
            return item
        item = type(item)(item)
        cleanup_details = item.pop("cleanup_details", None)
        if not isinstance(cleanup_details, dict):
            cleanup_details = {}
        item.pop("cleanup_user", None)
        # local history reason of a refusal; the server has the error text
        item.pop("refusal_reason", None)
        if isinstance(item.get("result"), dict):
            item["result"] = {
                path: result_for_server(result, cleanup_details.get(path))
                for path, result in item["result"].items()
            }
            # a file of a failed run has no result row: send why the run failed
            for path in cls._hit_paths(item.get("hits")):
                if path not in item["result"]:
                    entry = failure_for_server(cleanup_details.get(path))
                    if entry:
                        item["result"][path] = entry
        if item.get("hits"):
            item["hits"] = [
                cls._hit_for_server(hit) if isinstance(hit, Model) else hit
                for hit in item["hits"]
            ]
        return item

    @staticmethod
    def _hit_paths(hits):
        for hit in hits or ():
            path = (
                hit.get("orig_file")
                if isinstance(hit, dict)
                else getattr(hit, "orig_file", None)
            )
            if isinstance(path, str):
                yield path

    @classmethod
    def _hit_for_server(cls, hit):
        data = model_to_dict(hit)
        for field in cls.LOCAL_HIT_FIELDS:
            data.pop(field, None)
        return data

    @classmethod
    def _unit_size(cls, unit, is_dict, message):
        # Each hit carries a paired `result[orig_file]` entry (orig_file,
        # scan_path, before/after hashes). Count it so the byte budget covers
        # the whole per-hit payload, not just the hit itself.
        size = super()._unit_size(unit, is_dict, message)
        result = message.get("result") or {}
        orig_file = getattr(unit, "orig_file", None)
        if isinstance(result, dict) and orig_file in result:
            size += estimate_size({orig_file: result[orig_file]})
        elif isinstance(result, dict) and isinstance(orig_file, str):
            details = message.get("cleanup_details")
            if isinstance(details, dict):
                entry = failure_for_server(details.get(orig_file))
                if entry:
                    size += estimate_size({orig_file: entry})
        return size

    @classmethod
    def _split_items(cls, messages: List[Accumulatable]):
        for message in super()._split_items(messages):
            result = deepcopy(message.get("result", {}))
            if result and isinstance(result, dict):
                current_paths = {
                    hit.orig_file for hit in message.get("hits", [])
                }
                # after splitting messages, leave only the actual result
                # to avoid duplicates on the correlation server
                extra_paths = result.keys() - current_paths
                for path in extra_paths:
                    result.pop(path)
                message["result"] = result
            yield message


class MalwareProcessStopList(
    ShortenReprListMixin, Message, Reportable, Splittable
):
    """One record per process linked to a detected file during a cleanup,
    plus one counters record per run. Sent in every mode, flag on or off,
    so fleet data for a rule exists before the rule is enforced."""

    DEFAULT_METHOD = "MALWARE_PROCESS_STOP_LIST"

    LIST_SIZE = 5
    BATCH_SIZE = 1000
    BATCH_FIELD = "items"


class MalwareCleanup(Accumulatable):
    """
    Represents results of single cleanup
    """

    DEFAULT_METHOD = "MALWARE_CLEANUP"
    LIST_CLASS = MalwareCleanupList


class MalwareIgnorePathUpdated(Message):
    """Signal through a message bus that MalwareIgnorePath has been updated."""

    pass


class MalwareDatabaseScan(Message):
    """
    Represents results of a single MDS scan
    """

    args: Optional[List[str]]
    path: str
    real_path: Optional[str]
    scan_id: str
    type: Optional[str]
    error: Optional[str]
    started: int
    completed: int
    total_resources: int
    total_malicious: int
    hits: Set[MalwareDatabaseHitInfo]
    clean_apps: Set[MalwareDatabaseAppInfo]
    incomplete_apps: Set[MalwareDatabaseAppInfo]
    initiator: Optional[str]

    def __init__(
        self,
        *,
        args: Optional[List[str]],
        path: Optional[str],
        scan_id: str,
        type: Optional[str],
        real_path: Optional[str] = None,
        started: int = 0,
        completed: int = 0,
        total_resources: int = 0,
        total_malicious: int = 0,
        hits: Optional[Set[MalwareDatabaseHitInfo]] = None,
        clean_apps: Optional[Set[MalwareDatabaseAppInfo]] = None,
        incomplete_apps: Optional[Set[MalwareDatabaseAppInfo]] = None,
        error: Optional[str] = None,
        initiator: Optional[str] = None,
    ):
        super().__init__(
            args=args,
            path=path,
            real_path=real_path,
            scan_id=scan_id,
            type=type,
            started=started,
            completed=completed,
            total_resources=total_resources,
            total_malicious=total_malicious,
            hits=hits,
            clean_apps=clean_apps or set(),
            incomplete_apps=incomplete_apps or set(),
            error=error,
            initiator=initiator,
        )

    def update_with_report(self, report: MalwareDatabaseScanReport):
        self.update(
            started=report.started,
            completed=report.completed,
            total_resources=report.total_resources,
            total_malicious=report.total_malicious,
            hits=report.hits,
            clean_apps=report.clean_apps,
            incomplete_apps=report.incomplete_apps,
        )
        if report.errors:
            self.update_with_error(SCAN_FAILED)

    def update_with_error(self, message):
        return self.update(error=message)

    def __setitem__(self, key, value):
        raise NotImplementedError

    def __delitem__(self, key):
        raise NotImplementedError


class MalwareDatabaseRestore(Message):
    """
    Represents results of a single MDS restore
    """


class MalwareDatabaseRestoreTask(Message):
    """
    Represents a single MDS restore task
    """

    path: str
    app_name: str
    signature_id: str | None

    def __init__(self, path: str, app_name: str, signature_id: str | None):
        super().__init__(
            path=path, app_name=app_name, signature_id=signature_id
        )


class MalwareDatabaseCleanup(Message):
    """
    Represents results of a single mds cleanup
    """

    succeeded: Set[MalwareDatabaseHitInfo]
    failed: Set[MalwareDatabaseHitInfo]

    def __init__(
        self,
        succeeded: Set[MalwareDatabaseHitInfo],
        failed: Set[MalwareDatabaseHitInfo],
        scan_id: Optional[str] = None,
    ):
        super().__init__(
            succeeded=succeeded,
            failed=failed,
            scan_id=scan_id,
        )


class MalwareDatabaseCleanupFailed(Message):
    """
    Signifies an MDS cleanup failure
    """

    error: str

    def __init__(self, *, error: str):
        super().__init__(error=error)


class MalwareDatabaseRestoreFailed(Message):
    """
    Signifies an MDS restore failure
    """

    error: str

    def __init__(self, *, error: str, scan_id: str | None = None):
        """*scan_id*: the restore child that failed, when it is known."""
        super().__init__(error=error)
        if scan_id is not None:
            self["scan_id"] = scan_id


class RefreshImunifyPatchSubscription(Message):
    """Refresh Imunify Patch user subscription info."""


class MalwareCleanupRevertList(
    ShortenReprListMixin, Message, Reportable, Splittable
):
    DEFAULT_METHOD = "MALWARE_CLEANUP_REVERT_LIST"


class MalwareCleanupRevert(Accumulatable):
    LIST_CLASS = MalwareCleanupRevertList


class MalwareCleanupRevertFailed(Message, Reportable):
    DEFAULT_METHOD = "CLEANUP_FAILED"


class CheckDetachedScans(Message):
    DEFAULT_METHOD = "MALWARE_CHECK_DETACHED_SCANS"


class MalwareScanSlowFile(Message, Reportable):
    """A file a running scan has spent a long time on, reported by the
    scanner itself while it is still on it: a scan that never ends sends no
    summary, so this is the only record of the file it is stuck on.

    It identifies the content (sha256, size, extension), never the path.
    """

    DEFAULT_METHOD = "MALWARE_SCAN_SLOW_FILE"

    #: The fields kept from the scanner's message, and the longest string
    #: each may be; anything else is dropped.
    STR_FIELDS = {
        "scan_id": 64,
        "mode": 16,
        "sha256": 64,
        "ext": 16,
        "stage": 16,
        "version": 64,
        "signatures_version": 64,
    }
    INT_FIELDS = ("size", "elapsed_ms")

    @classmethod
    def from_sensor(cls, tokens: dict) -> "MalwareScanSlowFile":
        fields = {}
        for name, limit in cls.STR_FIELDS.items():
            value = tokens.get(name)
            fields[name] = value[:limit] if isinstance(value, str) else ""
        for name in cls.INT_FIELDS:
            value = tokens.get(name)
            ok = isinstance(value, int) and not isinstance(value, bool)
            fields[name] = max(0, value) if ok else 0
        # the time the sensor received it, stamped by the sensor itself
        received = tokens.get("timestamp")
        if type(received) in (int, float):  # not a bool
            fields["timestamp"] = received
        return cls(fields)


class VulnerabilityPatchTask(Message):
    DEFAULT_METHOD = "VULNERABILITY_PATCH_TASK"


class VulnerabilityPatchList(MalwareCleanupList):
    """Patched hits, sharing the cleanup list's wire method and its
    hits-plus-paired-result payload, so it splits by the same rules."""

    # unlike a cleanup, a patch run accumulates one message per user and has
    # never been capped by count -- only the byte budget bounds it
    LIST_SIZE = None


class VulnerabilityPatch(Accumulatable):
    DEFAULT_METHOD = "MALWARE_CLEANUP"
    LIST_CLASS = VulnerabilityPatchList


class VulnerabilityPatchRevertList(
    ShortenReprListMixin, Message, Reportable, Splittable
):
    DEFAULT_METHOD = "MALWARE_CLEANUP_REVERT_LIST"


class VulnerabilityPatchRevert(Accumulatable):
    LIST_CLASS = VulnerabilityPatchRevertList


class VulnerabilityPatchFailed(Message, Reportable):
    DEFAULT_METHOD = "CLEANUP_FAILED"


MSGS_WITHOUT_IP = [
    msg.DEFAULT_METHOD
    for msg in (
        Ack,
        CheckDetachedScans,
        MalwareScan,
        MalwareScanSummary,
        MalwareScanResult,
        MalwareScanComplete,
        MalwareCleanComplete,
        MalwareRestoreComplete,
        MalwareSendFiles,
        Noop,
        FilesUpdated,
    )
]
