"""
Groups model
"""

from django.core.validators import MinValueValidator
from django.db import models

from core.apps.eggs.models import party
from core.apps.eggs.models import product
from core.http.models import base


class Group(base.AbstractBaseModel):
    product_id = models.ForeignKey(
        to=product.Product, on_delete=models.CASCADE, related_name="groups"
    )
    party_id = models.ForeignKey(
        to=party.Party,
        on_delete=models.CASCADE,
        related_name="groups",
        blank=True,
        null=True,
    )
    entry_price = models.DecimalField(max_digits=30, decimal_places=2)
    unit_price = models.DecimalField(max_digits=30, decimal_places=2)
    wholesale_price = models.DecimalField(max_digits=30, decimal_places=2)
    quantity = models.IntegerField(
        validators=[MinValueValidator(0)], default=0
    )
    broken_eggs = models.IntegerField(
        validators=[MinValueValidator(0)], default=0
    )
    date = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=255, blank=True, null=True)

    def clean(self):
        if self.quantity < 0:
            raise ValueError("Quantity can't be negative")

    # def save(self, *args, **kwargs):
    #     self.party_id.count += self.quantity
    #     self.party_id.price += self.entry_price * self.quantity
    #     self.party_id.save()
    #     super().save(*args, **kwargs)

    def __str__(self) -> str:
        return f"{self.product_id}, Quantity - {self.quantity}"

    class Meta:
        verbose_name = "Group"
        verbose_name_plural = "Groups"
        db_table = "group"
