"""
Contains the PMVulnerability data class.
"""

from dataclasses import dataclass, asdict
from typing import Union, Literal
from datetime import datetime

from ...base.base_list import BaseList


@dataclass
class PMVulnerability:
    """
    Represents a QID with CVE details underneath
    in Patch Management.
    """

    id: int = None
    title: str = None
    cves: BaseList[str] = None
    detectedDate: Union[int, datetime] = None
    severity: int = None
    vulnType: str = None

    def __post_init__(self):
        if getattr(self, "detectedDate"):
            # Attempt to convert the detectedDate to a datetime object
            try:
                setattr(
                    self,
                    "detectedDate",
                    datetime.fromtimestamp(self.detectedDate / 1000),
                )
            # Unless a TypeError or OSError, indicating a way
            # off timestamp. Yes, this is a thing.
            except (TypeError, OSError):
                setattr(self, "detectedDate", None)

        if getattr(self, "cves"):
            setattr(self, "cves", BaseList(self.cves))

    def to_dict(self):
        return asdict(self)

    def __dict__(self):
        return self.to_dict()

    def __int__(self):
        return self.id

    def keys(self):
        return self.to_dict().keys()

    def values(self):
        return self.to_dict().values()

    def items(self):
        return self.to_dict().items()

    @classmethod
    def from_dict(cls, **kwargs):
        return cls(**kwargs)
