import datetime

from authentication.models import BaseModel, SystemUserProfile, SystemAddress, BankCard
from django.db import models


class LiveStockProvinceJahad(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="live_stock_jahad_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="live_stock_jahad_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="live_stock_jahad_bank_info",
        null=True
    )
    name = models.CharField(max_length=200, null=True)
    mobile = models.CharField(max_length=200, null=True)
    type = models.CharField(max_length=200, null=True)
    account = models.CharField(max_length=200, null=True)
    national_id = models.CharField(max_length=100, null=True)

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


class Union(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="union_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="union_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="union_bank_info",
        null=True
    )

    name = models.CharField(max_length=200, null=True)
    mobile = models.CharField(max_length=200, null=True)
    type = models.CharField(max_length=200, null=True)
    account = models.CharField(max_length=200, null=True)
    national_id = models.CharField(max_length=100, null=True)
    active = models.BooleanField(default=True)

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


class Cooperative(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="cooperative_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="cooperative_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="cooperative_bank_info",
        null=True
    )
    name = models.CharField(max_length=200, null=True)
    mobile = models.CharField(max_length=200, null=True)
    type = models.CharField(max_length=200, null=True)
    account = models.CharField(max_length=200, null=True)
    national_id = models.CharField(max_length=100, null=True)
    active = models.BooleanField(default=True)
    first_sub_cooperative_price = models.IntegerField(default=0)
    first_sub_cooperative_account = models.CharField(max_length=200, null=True)
    second_sub_cooperative_price = models.IntegerField(default=0)
    second_sub_cooperative_account = models.CharField(max_length=200, null=True)

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


class Rancher(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="rancher_user",
        null=True
    )
    # address = models.ForeignKey(
    #     SystemAddress,
    #     on_delete=models.CASCADE,
    #     related_name="rancher_address",
    #     null=True
    # )
    # user_bank_info = models.ForeignKey(
    #     BankCard,
    #     on_delete=models.CASCADE,
    #     related_name="rancher_bank_info",
    #     null=True
    # )
    # contractor = models.ForeignKey(
    #     Contractor,
    #     on_delete=models.CASCADE,
    #     related_name="rancher_contractor",
    #     null=True
    # )
    cooperative = models.ForeignKey(
        Cooperative,
        on_delete=models.CASCADE,
        related_name="rancher_cooperative",
        null=True
    )
    name = models.CharField(max_length=200, null=True)
    registering_user = models.CharField(max_length=200, null=True)
    lng = models.FloatField(default=0)
    lot = models.FloatField(default=0)
    mobile = models.CharField(max_length=200, null=True)
    fullname = models.CharField(max_length=200, null=True)
    city = models.CharField(max_length=200, null=True)
    herd_name = models.CharField(max_length=200, null=True)
    unit_id = models.CharField(max_length=100, null=True)
    postal_code = models.CharField(max_length=100, null=True)
    epidemiological_code = models.CharField(max_length=100, null=True)
    herd_code = models.CharField(max_length=100, null=True)
    national_id = models.CharField(max_length=100, null=True)
    allow_buy = models.BooleanField(default=True)
    weight_allocation_light = models.IntegerField(default=0)
    weight_allocation_heavy = models.IntegerField(default=0)
    weight_quota_heavy = models.IntegerField(default=0)
    weight_quota_light = models.IntegerField(default=0)
    total_weight = models.BigIntegerField(default=0)
    contractor_code = models.CharField(max_length=100, null=True)
    sheep = models.IntegerField(default=0)
    goat = models.IntegerField(default=0)
    cow = models.IntegerField(default=0)
    horse = models.IntegerField(default=0)
    camel = models.IntegerField(default=0)
    light_livestock = models.IntegerField(default=0)
    heavy_livestock = models.IntegerField(default=0)
    has_script = models.BooleanField(default=False)
    active = models.BooleanField(default=True)
    type = models.CharField(max_length=200, default='rural')
    industrial = models.BooleanField(default=False)
    dhi_amount = models.IntegerField(default=0)


    def save(self, *args, **kwargs):
        # live_stocks = LiveStock.objects.filter(trash=False, herd_code=self.herd_code).only('type')
        # light_livestock = live_stocks.filter(type__in=('بز', 'گوسفند')).count()
        # heavy_livestock = live_stocks.filter(type__in=('گاو', 'اسب', 'شتر')).count()
        # product = LiveStockProduct.objects.filter(trash=False, name='سبوس').first()
        # self.weight_quota_heavy=product.heavy_wight * heavy_livestock
        # self.weight_quota_light=product.light_wight * light_livestock
        super(Rancher, self).save(*args, **kwargs)


class LiveStock(BaseModel):
    rancher = models.ForeignKey(Rancher, on_delete=models.CASCADE, null=True)
    national_id_livestock_code = models.CharField(max_length=200, null=True)
    herd_code = models.CharField(max_length=100, null=True)
    type = models.CharField(max_length=200, null=True)
    birth_day = models.CharField(max_length=200, null=True)
    gender = models.CharField(max_length=100, null=True)
    contractor_code = models.CharField(max_length=100, null=True)
    unique_identifier = models.CharField(max_length=100, null=True)
    agent = models.CharField(max_length=100, null=True)
    registering_user = models.CharField(max_length=200, null=True)
    registering_date = models.CharField(max_length=200, null=True)
    active = models.BooleanField(default=True)
    birth_day_gh = models.DateTimeField(null=True)
    registering_date_gh = models.DateTimeField(null=True)
    archive=models.BooleanField(default=False)
    age_of_archive=models.IntegerField(default=1)
    archiver=models.CharField(max_length=250,null=True)
    archive_date=models.DateTimeField(null=True)
    returner_from_archive=models.CharField(max_length=250,null=True)
    return_from_archive_date=models.DateTimeField(null=True)

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


class LiveStockProduct(BaseModel):
    name = models.CharField(max_length=200, null=True)
    image = models.CharField(max_length=500, null=True)
    unit = models.CharField(max_length=200, null=True)
    price = models.IntegerField(default=0)
    light_wight = models.IntegerField(default=0)
    heavy_wight = models.IntegerField(default=0)
    company_price = models.IntegerField(default=0)
    shipping_price = models.IntegerField(default=0)
    union_price = models.IntegerField(default=0)
    cooperative_price = models.IntegerField(default=0)
    light_wight_industrial = models.IntegerField(default=0)
    heavy_wight_industrial = models.IntegerField(default=0)
    light_wight_dha = models.IntegerField(default=0)
    heavy_wight_dha = models.IntegerField(default=0)

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


class CooperativeProductsShare(BaseModel):
    cooperative = models.ForeignKey(
        Cooperative,
        on_delete=models.CASCADE,
        related_name="cooperative_share_product",
        null=True
    )
    product = models.ForeignKey(
        LiveStockProduct,
        on_delete=models.CASCADE,
        related_name="cooperative_share_product",
        null=True
    )
    price = models.IntegerField(default=0)
    shipping_price = models.IntegerField(default=0)
    union_price = models.IntegerField(default=0)
    cooperative_price = models.IntegerField(default=0)
    company_price = models.IntegerField(default=0)

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


class LiveStockRolseProduct(BaseModel):
    parent_product = models.ForeignKey(
        LiveStockProduct,
        on_delete=models.CASCADE,
        related_name="parents_product",
        null=True
    )
    jahad = models.ForeignKey(
        LiveStockProvinceJahad,
        on_delete=models.CASCADE,
        related_name="jahad_product",
        null=True
    )
    union = models.ForeignKey(
        Union,
        on_delete=models.CASCADE,
        related_name="union_product",
        null=True
    )
    cooperative = models.ForeignKey(
        Cooperative,
        on_delete=models.CASCADE,
        related_name="cooperative_product",
        null=True
    )
    # rancher = models.ForeignKey(
    #     Rancher,
    #     on_delete=models.CASCADE,
    #     related_name="rancher_product",
    #     null=True
    # )
    name = models.CharField(max_length=200, null=True)
    total_weight = models.BigIntegerField(default=0)
    total_receipt_weight = models.BigIntegerField(default=0)
    total_allocated_weight = models.BigIntegerField(default=0)
    total_remain_weight = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        self.total_remain_weight = self.total_weight - self.total_allocated_weight
        super(LiveStockRolseProduct, self).save(*args, **kwargs)


class LiveStockAllocations(BaseModel):
    product = models.ForeignKey(
        LiveStockRolseProduct,
        on_delete=models.CASCADE,
        related_name="product_allocation",
        null=True
    )
    jahad = models.ForeignKey(
        LiveStockProvinceJahad,
        on_delete=models.CASCADE,
        related_name="jahad_allocation",
        null=True
    )
    union = models.ForeignKey(
        Union,
        on_delete=models.CASCADE,
        related_name="union_allocation",
        null=True
    )
    cooperative = models.ForeignKey(
        Cooperative,
        on_delete=models.CASCADE,
        related_name="cooperative_allocation",
        null=True
    )
    state = models.CharField(max_length=200, default='pending')
    weight = models.BigIntegerField(default=0)
    real_weight = models.BigIntegerField(default=0)
    date = models.DateTimeField(default=datetime.datetime.now)
    allocate_from = models.CharField(max_length=200, null=True)
    allocate_to = models.CharField(max_length=200, null=True)
    place = models.TextField(null=True)
    charge = models.BooleanField(default=False)
    code = models.CharField(max_length=200, null=True)
    description = models.TextField(null=True, blank=True)

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


class Contractor(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="contractor_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="contractor_user",
        null=True
    )
    cooperative = models.ForeignKey(
        Cooperative,
        on_delete=models.CASCADE,
        related_name="contractor_cooperative",
        null=True
    )
    contractor_code = models.CharField(max_length=100, null=True)
    fullname = models.CharField(max_length=200, null=True)
    entity_code = models.CharField(max_length=200, null=True)
    national_id = models.CharField(max_length=100, null=True)
    company_name = models.CharField(max_length=200, null=True)
    number_of_rancher = models.IntegerField(default=0)
    heavy_livestock = models.IntegerField(default=0)
    light_livestock = models.IntegerField(default=0)

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


class PosSeller(BaseModel):
    cooperative = models.ForeignKey(
        Cooperative,
        on_delete=models.CASCADE,
        related_name="seller_pos_cooperative",
        null=True
    )
    full_name=models.CharField(max_length=200,null=True)
    mobile=models.CharField(max_length=50,null=True)
    national_id=models.CharField(max_length=50,null=True)
    city=models.CharField(max_length=100,null=True)

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