from django.db import models


class SDEIcon(models.Model):
    id = models.IntegerField(primary_key=True)
    iconFile = models.CharField()
    description = models.CharField()


class SDECategory(models.Model):
    id = models.IntegerField(primary_key=True)
    icon = models.ForeignKey(SDEIcon, related_name="categories", null=True, on_delete=models.SET_NULL)
    name = models.CharField()
    published = models.BooleanField()


class SDEGroup(models.Model):
    id = models.IntegerField(primary_key=True)
    category = models.ForeignKey(SDECategory, related_name="groups", on_delete=models.CASCADE)
    name = models.CharField()
    published = models.BooleanField()
    useBasePrice = models.BooleanField()
    fittableNonSingleton = models.BooleanField()
    anchored = models.BooleanField()
    anchorable = models.BooleanField()
    icon = models.ForeignKey(SDEIcon, related_name="groups", null=True, on_delete=models.SET_NULL)


class SDEMarektGroup(models.Model):
    id = models.IntegerField(primary_key=True)
    icon = models.ForeignKey(SDEIcon, null=True, on_delete=models.SET_NULL)
    name = models.CharField()
    description = models.CharField(default="")
    hasTypes = models.BooleanField()
    parent_marketgroup = models.ForeignKey("self", null=True, related_name="child_marketgroups", on_delete=models.CASCADE)


class SDEMetaGroup(models.Model):
    id = models.IntegerField(primary_key=True)
    icon = models.ForeignKey(SDEIcon, related_name="metagroups", null=True, on_delete=models.SET_NULL)
    name = models.CharField()
    iconSuffix = models.CharField()


class SDEType(models.Model):
    id = models.IntegerField(primary_key=True)
    group = models.ForeignKey(SDEGroup, related_name="types", on_delete=models.CASCADE)
    marketgroup = models.ForeignKey(SDEMarektGroup, related_name="types", on_delete=models.SET_NULL, null=True)
    metagroup = models.ForeignKey(SDEMetaGroup, related_name="types", on_delete=models.SET_NULL, null=True)
    name = models.CharField()
    description = models.CharField()
    published = models.BooleanField()
    basePrice = models.FloatField()
    icon = models.ForeignKey(SDEIcon, related_name="types", null=True, on_delete=models.SET_NULL)
    volume = models.FloatField()
    portionSize = models.IntegerField()
    materials = models.ManyToManyField("self", through="SDETypeMaterial", symmetrical=False, related_name="material_of")

    def __str__(self):
        return self.name


class SDETypeMaterial(models.Model):
    type = models.ForeignKey(SDEType, on_delete=models.CASCADE, related_name="typematerials")
    material_type = models.ForeignKey(SDEType, on_delete=models.CASCADE, related_name="typematerials_of")

    quantity = models.IntegerField()
