from apps.product import models as product_models
from apps.authentication.models import User
from apps.core.models import BaseModel
from django.db import models


class InventoryEntry(BaseModel):
    distribution = models.ForeignKey(
        product_models.QuotaDistribution,
        on_delete=models.CASCADE,
        related_name='inventory_entry',
        null=True
    )
    organization = models.ForeignKey(
        product_models.Organization,
        on_delete=models.CASCADE,
        related_name="inventory",
        null=True
    )
    weight = models.PositiveBigIntegerField(default=0)
    balance = models
    lading_number = models.CharField(max_length=50, null=True)
    delivery_address = models.TextField(blank=True, null=True)
    document = models.CharField(max_length=250, null=True)
    is_confirmed = models.BooleanField(default=False)
    notes = models.TextField(blank=True, null=True)

    @property
    def total_sold(self):
        return self.inventory_sales.aggregate(total=models.Sum('weight'))['total'] or 0

    @property
    def remaining_weight(self):
        return self.weight - self.total_sold

    def __str__(self):
        return f"distribution: {self.distribution.distribution_id}-{self.organization.name}"

    def save(self, *args, **kwargs):
        super(InventoryEntry, self).save(*args, **kwargs)


class InventoryQuotaSaleTransaction(BaseModel):
    transaction_id = models.CharField(max_length=50, null=True)
    seller_organization = models.ForeignKey(
        product_models.Organization,
        on_delete=models.CASCADE,
        related_name='inventory_sales',
        null=True
    )
    quota_distribution = models.ForeignKey(
        product_models.QuotaDistribution,
        on_delete=models.CASCADE,
        related_name='inventory_sales',
        null=True
    )
    inventory_entry = models.ForeignKey(
        InventoryEntry,
        on_delete=models.CASCADE,
        related_name='inventory_sales',
        null=True
    )
    buyer_user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='buyer_sale_transactions',
        null=True
    )
    seller_user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='seller_sale_transactions',
        null=True
    )
    weight = models.DecimalField(max_digits=12, decimal_places=2, null=True)
    delivery_address = models.TextField(blank=True, null=True)
    product = models.ForeignKey(
        product_models.Product,
        on_delete=models.CASCADE,
        related_name='sale_transactions',
        null=True
    )
    transaction_price = models.PositiveBigIntegerField(default=0)
    description = models.TextField(blank=True, null=True)
    herd_owners_number = models.PositiveBigIntegerField(default=0)
    transactions_number = models.PositiveBigIntegerField(default=0)
    sale_status = models.BooleanField(default=False)
    is_active = models.BooleanField(default=0)

    def buyers_count(self):
        """ number of buyers from specific inventory """

        unique_buyers_count = self.objects.filter(
            inventory_entry=self.inventory_entry
        ).values('buyer_user').distinct().count()

        return unique_buyers_count

    def __str__(self):
        return f"Inventory Sale: {self.transaction_id}-{self.quota_distribution.distribution_id}"

    def save(self, *args, **kwargs):
        super(InventoryQuotaSaleTransaction, self).save(*args, **kwargs)
