"""
Courier Product model
"""

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

from core.apps.eggs.models import courier, group
from core.http.models import base


class CourierProduct(base.AbstractBaseModel):
    group_id = models.ForeignKey(
        to=group.Group, on_delete=models.CASCADE, related_name="courier_groups"
    )
    count = models.IntegerField(
        validators=[MinValueValidator(0)], verbose_name="Count of eggs"
    )
    courier_id = models.ForeignKey(
        to=courier.Courier,
        on_delete=models.CASCADE,
        related_name="courier_products",
    )
    return_eggs = models.IntegerField(
        default=0,
        verbose_name="Return eggs",
        validators=[MinValueValidator(0)],
        null=True,
        blank=True,
    )

    def clean(self):
        if self.count < 0:
            raise ValueError("Count must be greater than 0")

    def __str__(self):
        return f"Courier - {self.courier_id}, group_id - {self.group_id}"

    class Meta:
        verbose_name = "CourierProduct"
        verbose_name_plural = "CourierProducts"
        db_table = "courier_product"
