from .. import BaseModel, Base
from sqlalchemy import Column, Integer, String, Boolean, ForeignKey, Table, Text
from sqlalchemy.orm import relationship
from .. import JsonType

vulnerability_port_table = Table(
    "vulnerability_service_table",
    Base.metadata,
    Column("port_id", Integer, ForeignKey("port.id")),
    Column("vulnerability_id", Integer, ForeignKey("vulnerability.id")),
)


class Vulnerability(BaseModel):
    __tablename__ = "vulnerability"
    __repr_attrs__ = ["name"]
    id = Column(Integer, primary_key=True)
    name = Column(String(128))
    ports = relationship(
        "Port", secondary=vulnerability_port_table, backref="vulnerabilities"
    )
    description = Column(Text)
    remediation = Column(Text)
    ip_address = Column(Integer, ForeignKey("ipaddress.id"))
    severity = Column(Integer)
    exploitable = Column(Boolean(create_constraint=False), default=True)
    exploit_reference = Column(JsonType)

    def __repr__(self):
        return "Vulnerability: {}".format(self.name)