from django.db import models
from django.utils.translation import gettext_lazy

from console.models import UniqueNameDescriptionModel, UpdatedNameDescriptionModel, SensorConnectedMixin
from core.validators import mac_address_validator
from incident.models import Incident


class OperatingSystem(UniqueNameDescriptionModel):
    """ One item in operation systems list """

    class Meta:
        verbose_name = gettext_lazy('Operating system')


class AssetListGroup(UniqueNameDescriptionModel):
    """ Asset list display group """
    collapsed = models.BooleanField(default=False)


class AssetManufacturer(UniqueNameDescriptionModel):
    """ Asset manufacturer """

    class Meta:
        verbose_name = gettext_lazy('Asset manufacturer')


class Asset(SensorConnectedMixin, UpdatedNameDescriptionModel):
    """ Model for one asset """

    class AllowStatus(models.IntegerChoices):
        NEW = 0, gettext_lazy('New asset')
        ALLOWED = 1, gettext_lazy('Allowed asset')

    class AssetType(models.TextChoices):
        USER = 'user', gettext_lazy('User')
        ARMA = 'arma_industrial_firewall', gettext_lazy('ARMA industrial firewall')
        PLC = 'plc', gettext_lazy('PLC')
        PC = 'pc', gettext_lazy('PC')
        SERVER = 'server', gettext_lazy('Server')
        NETWORK_DEVICE = 'network_device', gettext_lazy('Network device')

    manufacturer = models.ForeignKey(AssetManufacturer,
                                     verbose_name=gettext_lazy('Manufacturer'),
                                     on_delete=models.SET_NULL,
                                     blank=True,
                                     null=True)
    model = models.CharField(blank=True,
                             null=True,
                             max_length=150,
                             verbose_name=gettext_lazy('Model'),
                             help_text=gettext_lazy('Asset model'))
    ip = models.GenericIPAddressField(verbose_name=gettext_lazy('IP'), help_text=gettext_lazy("Asset's IP address"))
    mac = models.CharField(blank=True,
                           null=True,
                           max_length=17,
                           verbose_name=gettext_lazy('MAC'),
                           help_text=gettext_lazy("Asset's MAC address"),
                           validators=[mac_address_validator])
    os = models.ForeignKey(OperatingSystem,
                           on_delete=models.SET_NULL,
                           verbose_name=gettext_lazy('OS'),
                           help_text=gettext_lazy('Operation systems, found on asset'),
                           blank=True,
                           null=True)
    ports = models.JSONField(verbose_name=gettext_lazy('Ports'),
                             help_text=gettext_lazy('List of open ports'),
                             null=True,
                             blank=True, default="[]")
    incidents = models.ManyToManyField(Incident, verbose_name=gettext_lazy('Incidents'), blank=True)
    group = models.ForeignKey(AssetListGroup,
                              on_delete=models.SET_NULL,
                              blank=True,
                              null=True,
                              verbose_name=gettext_lazy('Group'))
    asset_type = models.CharField(choices=AssetType.choices,
                                  max_length=128,
                                  verbose_name=gettext_lazy('Asset type'),
                                  blank=True,
                                  null=True)
    status = models.IntegerField(choices=AllowStatus.choices,
                                 verbose_name=gettext_lazy('Asset status'),
                                 help_text=gettext_lazy('Asset allow status'),
                                 default=AllowStatus.NEW,
                                 blank=True)

    @property
    def manufacturer_name(self):
        return self.manufacturer.name

    def __str__(self):
        return self.name
