from django.db import models
from django.urls import reverse
from Item.models import PriceLevel

class Customer(models.Model):
    MR = 'Mr'
    MRS = 'Mrs'

    SALUTATION_CHOICE = [
        (MR, 'Mr.'),
        (MRS, 'Mrs.'),
    ]
    
    CustomerName = models.CharField(max_length=80)
    CustomerFullName = models.CharField(max_length=160, unique=True)
    CustomerIsActive = models.BooleanField(default=True)
    Parent = models.ForeignKey("Customer", on_delete=models.DO_NOTHING, blank=True, null=True)
    Sublevel = models.PositiveSmallIntegerField(default=0)
    CompanyName = models.CharField(max_length=80)
    Salutation = models.CharField(max_length=4, choices=SALUTATION_CHOICE)
    FirstName = models.CharField(max_length=80)
    MiddleName = models.CharField(max_length=80, blank=True, null=True)
    LastName = models.CharField(max_length=80, blank=True, null=True)
    BillAddr1 = models.CharField(max_length=80)
    BillAddr2 = models.CharField(max_length=80, blank=True, null=True)
    BillAddr3 = models.CharField(max_length=80, blank=True, null=True)
    BillAddr4 = models.CharField(max_length=80, blank=True, null=True)
    BillAddr5 = models.CharField(max_length=80, blank=True, null=True)
    BillCity = models.CharField(max_length=80, blank=True, null=True)
    BillState = models.CharField(max_length=80, blank=True, null=True)
    BillPostalCode = models.CharField(max_length=80, blank=True, null=True)
    BillCountry = models.CharField(max_length=80, default="Indonesia")
    BillNote = models.CharField(max_length=80, blank=True, null=True)
    ShipAddr1 = models.CharField(max_length=80, blank=True, null=True)
    ShipAddr2 = models.CharField(max_length=80, blank=True, null=True)
    ShipAddr3 = models.CharField(max_length=80, blank=True, null=True)
    ShipAddr4 = models.CharField(max_length=80, blank=True, null=True)
    ShipAddr5 = models.CharField(max_length=80, blank=True, null=True)
    ShipCity = models.CharField(max_length=80, blank=True, null=True)
    ShipState = models.CharField(max_length=80, blank=True, null=True)
    ShipPostalCode = models.CharField(max_length=80, blank=True, null=True)
    ShipCountry = models.CharField(max_length=80, blank=True, null=True)
    ShipNote = models.CharField(max_length=200, blank=True, null=True)
    Phone = models.CharField(max_length=80, blank=True, null=True)
    Contact = models.CharField(max_length=80)
    # CustomerTypeRefFullName
    # TermsRefFullName
    # SalesRepRefFullName
    # Balance
    # TotalBalance
    # SalesTaxCodeRefFullName
    # ItemSalesTaxRefFullName
    # CreditCardNo
    # ExpirationMonth
    # ExpirationYear
    # NameOnCard
    # CreditCardAddress
    # CreditCardPostalCode
    # JobStatus
    # JobStartDate
    # JobProjectedEndDate
    # JobEndDate
    # JobTypeRefFullName
    Notes = models.CharField(max_length=200, blank=True, null=True)
    PriceLevelRefFullName = models.ForeignKey("Item.PriceLevel", related_name="PriceList", on_delete=models.SET_NULL, blank=True, null=True)
    # CurrencyRefFullName
    AltPhone = models.CharField(max_length=25, blank=True, null=True)
    Fax = models.CharField(max_length=25, blank=True, null=True)
    Email = models.CharField(max_length=50, blank=True, null=True)
    AltContact = models.CharField(max_length=50, blank=True, null=True)
    # ResaleNumber
    # AccountNumber
    CreditLimit = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    # PreferredPaymentMethodRefFullName
    NPWP = models.CharField(max_length=20, blank=True, null=True)
    KTP = models.CharField(max_length=16, blank=True, null=True)
    DMS_Cust_Name = models.CharField(max_length=80, blank=True, null=True)
    DMS_Cust_Code = models.CharField(max_length=80, blank=True, null=True)
    Special_Cust = models.BooleanField(default=False)
    EFaktur_Name = models.CharField(max_length=80, blank=True, null=True)
    Efaktur_Address = models.CharField(max_length=80, blank=True, null=True)
    Coordinates = models.CharField(max_length=30, blank=True, null=True)
    TimeCreated = models.DateTimeField(auto_now_add=True)
    TimeModified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.CustomerName}; {self.PriceLevelRefFullName}"
    
    def get_field_name(obj):
        # return [(f.name, f.value_to_string(obj)) for f in obj._meta.fields]   #get value convert it to string
        return [(f.verbose_name, f.name, f.value_from_object(obj)) for f in obj._meta.fields]
    
    def get_absolute_url(self):
        return reverse('Customer:edit_customer', args=[str(self.id)])



