from django.db import models

from apps.authentication import models as auth_models
from apps.authentication.models import Organization
from apps.core.models import BaseModel


class Herd(BaseModel):
    owner = models.ForeignKey(
        auth_models.User,
        on_delete=models.CASCADE,
        related_name='herd',
        null=True
    )
    rancher = models.ForeignKey(
        'Rancher',
        on_delete=models.CASCADE,
        related_name='herd',
        null=True
    )
    cooperative = models.ForeignKey(
        auth_models.Organization,
        on_delete=models.CASCADE,
        related_name='herd',
        null=True
    )
    name = models.CharField(max_length=200)
    photo = models.CharField(max_length=200, null=True)
    code = models.CharField(max_length=20)
    heavy_livestock_number = models.BigIntegerField(default=0)
    light_livestock_number = models.BigIntegerField(default=0)
    heavy_livestock_quota = models.BigIntegerField(default=0)
    light_livestock_quota = models.BigIntegerField(default=0)
    province = models.ForeignKey(
        auth_models.Province,
        on_delete=models.CASCADE,
        related_name='herd_province',
        null=True
    )
    city = models.ForeignKey(
        auth_models.City,
        on_delete=models.CASCADE,
        related_name='herd_city',
        null=True
    )
    postal = models.CharField(
        max_length=20,
        help_text="herd postal code", null=True
    )
    institution = models.CharField(
        max_length=20,
        help_text="herd institution code", null=True
    )
    epidemiologic = models.CharField(max_length=18, null=True)  # noqa
    contractor = models.ForeignKey(
        auth_models.Organization,
        on_delete=models.CASCADE,
        related_name="herd_contractor",
        null=True
    )
    latitude = models.FloatField(default=0)
    longitude = models.FloatField(default=0)
    unit_unique_id = models.CharField(max_length=20, null=True)
    activity_types = (
        ("I", "Industrial"),
        ("V", "Village"),
        ("N", "Nomadic")
    )
    activity = models.CharField(
        choices=activity_types,
        max_length=1,
        null=True
    )
    activity_state = models.BooleanField(default=False)
    operating_license_state = models.BooleanField(default=False)
    capacity = models.IntegerField(default=0)

    def __str__(self):
        return f'{self.name}-{self.code}'

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


class Rancher(BaseModel):
    organization = models.ForeignKey(
        Organization,
        on_delete=models.CASCADE,
        related_name='ranchers',
        null=True,
        help_text="connect ranchers to their specific Taavoni"  # noqa
    )
    ranching_farm = models.CharField(max_length=150, null=True)
    union_name = models.CharField(max_length=50, null=True, blank=True)
    union_code = models.CharField(max_length=50, null=True, blank=True)
    activity_types = (
        ("I", "Industrial"),
        ("V", "Village"),
        ("N", "Nomadic")
    )
    activity = models.CharField(
        choices=activity_types,
        max_length=1,
        null=True
    )
    rancher_types = (
        ('N', 'Natural'),
        ('L', 'Legal')
    )
    rancher_type = models.CharField(
        max_length=1,
        choices=rancher_types,
        default='N',
        help_text="N is natural & L is legal"
    )
    heavy_livestock_number = models.BigIntegerField(default=0)
    light_livestock_number = models.BigIntegerField(default=0)
    herd_code = models.CharField(max_length=100, null=True)
    first_name = models.CharField(max_length=150, null=True)
    last_name = models.CharField(max_length=150, null=True)
    mobile = models.CharField(max_length=25, null=True)
    national_code = models.CharField(max_length=50, null=True)
    birthdate = models.DateTimeField(null=True)
    nationality = models.CharField(max_length=50, null=True)
    address = models.TextField(blank=True)
    province = models.ForeignKey(
        auth_models.Province,
        on_delete=models.CASCADE,
        related_name='ranchers',
        null=True
    )
    city = models.ForeignKey(
        auth_models.City,
        on_delete=models.CASCADE,
        related_name='ranchers',
        null=True
    )
    without_herd = models.BooleanField(default=False)
    ignore_purchase_limit = models.BooleanField(
        default=True, help_text="if its true rancher has not buy limitations"
    )
    dhi_state = models.BooleanField(default=False)

    def __str__(self):
        return f'rancher: {self.first_name} {self.last_name}'

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


class RancherOrganizationLink(BaseModel):
    organization = models.ForeignKey(
        Organization,
        on_delete=models.CASCADE,
        related_name='rancher_links',
        null=True
    )
    rancher = models.ForeignKey(
        Rancher,
        on_delete=models.CASCADE,
        related_name='organization_links',
        null=True
    )

    def __str__(self):
        return f'rancher: {self.rancher.id} - organization: {self.organization.id}'

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


class HerdRancherTemporary(BaseModel):
    herd_code = models.CharField(max_length=150, null=True)
    herd_name = models.CharField(max_length=150, null=True)
    epidemiologic = models.CharField(max_length=150, null=True)  # noqa
    latitude = models.FloatField(default=0)
    longitude = models.FloatField(default=0)
    unit_unique_id = models.CharField(max_length=150, null=True)
    postal_code = models.CharField(max_length=150, null=True)
    rancher_national_code = models.CharField(max_length=150, null=True)
    rancher_name = models.CharField(max_length=150, null=True)
    mobile = models.CharField(max_length=150, null=True)
    agent_code = models.CharField(max_length=150, null=True)
    registerer_user = models.CharField(max_length=150, null=True)
    sync_status = models.CharField(max_length=50, null=True)
    city = models.CharField(max_length=150, null=True)

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