from packageurl import PackageURL

from .base import Serializable


class Vulnerability(Serializable):
    def __init__(self,
                 id: str,
                 purl: PackageURL,
                 description: str,
                 reference: str = "Unknown",
                 type: str = "Malware"
                 ):
        self.id = id
        self.purl = purl
        self.description = description
        self.reference = reference
        self.type = type

    @classmethod
    def create(cls, id, purl, description, reference="Unknown", type="Malware"):
        return cls(purl, description, reference, type)

    def to_dict(self):
        return {
            "id": self.id,
            "purl": str(self.purl),
            "type": self.type,
            "description": self.description,
            "reference": self.reference
        }

    @classmethod
    def from_dict(cls, data):
        reference = data.get("reference", "Unknown")
        type = data.get("type", "Unknown")
        return Vulnerability(data['id'], data["purl"], data["description"], reference, type)
