from django.db import models
from common.models import SluggedModel, BaseModel
from django.contrib.auth import get_user_model
from simple_history.models import HistoricalRecords
from locations.models import Affairs, Broker, County
import utm
from uuid import uuid4
import os

class WaterMeterManufacturer(BaseModel):
    name = models.CharField(
        max_length=20,
        verbose_name="نام شرکت سازنده کنتور آب",
        null=True,
        blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "شرکت سازنده کنتور آب"
        verbose_name_plural = "شرکت‌های سازنده کنتور آب"


class Well(SluggedModel):
    def path_and_rename(self, filename):
        upload_to = "representative_letters"
        ext = filename.split(".")[-1]
        # create unique filename
        filename = f"{uuid4().hex}.{ext}"
        # return the whole path to the file
        return os.path.join(upload_to, filename)
    
    Hemisphere_Choices = [
        ('N', 'شمال'),
        ('S', 'جنوب'),
    ]

    representative = models.ForeignKey(
        get_user_model(),
        on_delete=models.SET_NULL,
        verbose_name="نماینده",
        related_name="wells",
        null=True,
    )
    
    water_subscription_number = models.CharField(
        max_length=20,
        verbose_name="شماره اشتراک آب",
        unique=True
        )
    electricity_subscription_number = models.CharField(
        max_length=20,
        verbose_name="شماره اشتراک برق",
        null=True,
        unique=True
        )

    water_meter_serial_number = models.CharField(
        max_length=20,
        verbose_name="سریال کنتور آب",
        null=True,
        blank=True
        )
    water_meter_old_serial_number = models.CharField(
        max_length=20,
        verbose_name="سریال کنتور قدیمی آب",
        null=True,
        blank=True
        )
    water_meter_manufacturer = models.ForeignKey(
        WaterMeterManufacturer,
        on_delete=models.SET_NULL,
        verbose_name="شرکت سازنده کنتور آب",
        null=True,
        blank=True
        )

    utm_x = models.DecimalField(
        max_digits=10,
        decimal_places=6,
        verbose_name="X UTM",
        null=True,
        blank=True
        )
    utm_y = models.DecimalField(
        max_digits=10,
        decimal_places=6,
        verbose_name="Y UTM",
        null=True,
        blank=True
        )
    utm_zone = models.PositiveIntegerField(
        verbose_name="زون UTM",
        null=True,
        blank=True,
        default=40
        )
    utm_hemisphere = models.CharField(
        max_length=1,
        verbose_name="نیمکره UTM",
        null=True,
        blank=True,
        choices=Hemisphere_Choices,
        default='N'
        )

    well_power = models.PositiveIntegerField(
        verbose_name="قدرت چاه",
        null=True,
        blank=True
        )

    reference_letter_number = models.CharField(
        max_length=20,
        verbose_name="شماره معرفی نامه",
        null=True,
        blank=True
        )
    reference_letter_date = models.DateField(
        verbose_name="تاریخ معرفی نامه",
        null=True,
        blank=True
        )
    representative_letter_file = models.FileField(
        upload_to=path_and_rename,
        verbose_name="نامه نمایندگی",
        null=True,
        blank=True
        )


    affairs = models.ForeignKey(
        Affairs,
        on_delete=models.SET_NULL,
        verbose_name="امور",
        null=True,
        blank=True
        )
    county = models.ForeignKey(
        County,
        on_delete=models.SET_NULL,
        verbose_name="شهرستان",
        null=True,
        blank=True
        )  
    broker = models.ForeignKey(
        Broker,
        on_delete=models.SET_NULL,
        verbose_name="کارگزار",
        null=True,
        blank=True
        )


    history = HistoricalRecords()

    class Meta:
        verbose_name = "چاه"
        verbose_name_plural = "چاه‌ها"
    
    def __str__(self):
        return self.water_subscription_number
    
    def lat_long(self):
        if self.utm_x and self.utm_y and self.utm_zone and self.utm_hemisphere:
            northern = False if self.utm_hemisphere == 'S' else True
            return utm.to_latlon(self.utm_x, self.utm_y, self.utm_zone, northern=northern)
        return None