from django.db import models
from baseapp.models import patient, employee
from datetime import datetime
from django.utils import timezone 
from baseapp.models import treatment
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
# Create your models here.


class observation(models.Model):
    class Meta:
        verbose_name_plural = "Observations"

    report_name =  models.CharField(max_length=50, help_text='Enter report name',null=True,verbose_name='Report Name')
    report_file = models.FileField(help_text='Upload a report file',blank=True,null=True,verbose_name='Report File')
    report_vaue = models.CharField(max_length=100, help_text='Enter report value',blank=True,null=True,verbose_name='Report Value')
    fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING, null=True)
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING, null=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    date_modified = models.DateTimeField(default=timezone.now)
    def __str__(self):
        return self.report_name 

class nutritional_value(models.Model):
    class Meta:
        verbose_name_plural = "Nutrition Values"
    height = models.FloatField( help_text="Patient's height in centimeters",default=0.0,null=True,verbose_name='Height(cm') 
    weight = models.FloatField( help_text="Patient's weight in kilograms.",default=0.0,null=True,verbose_name='Weight(kg)')
    BMI  = models.FloatField( help_text="Patient's weight in kilograms.",default=0.0,null=True,verbose_name='BMI')
    Waistratio  = models.FloatField( help_text="WHR = Waist circumference in cms/ hip circumference in cms",default=0.0,null=True,verbose_name='Waist to Hip ratio')
    
    fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING, null=True)
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING,null=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    date_modified = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return str(self.height)
    # def save(self, *args, **kwargs):
    #     dbl_base = self.height/100
    #     dbl_BMI = self.weight/(dbl_base*dbl_base)
    #     nutrvalue = nutritional_value(BMI = dbl_BMI, **kwargs)
    #     nutrvalue.save()
#post_save.connect(BMI_calculate,sender=nutritional_value)


    
    
class blood_Pressure(models.Model):

    class Meta:
        verbose_name_plural = "Blood Pressure"

    position     = (('Sitting', 'Sitting'),('Recumbent', 'Recumbent'),('Unknown', 'Unknown'),('Other', 'Other'),('Standing', 'Standing'),("Fowler's Position","Fowler's Position"))

    sys_bld_pressure = models.FloatField( help_text="A patient's systolic blood pressure measurement (taken with a manual cuff in either a sitting or standing position)",blank=True,null=True,verbose_name='Systolic Blood Pressure(mmHg)') 
    sys_bld_abnormal = models.BooleanField(verbose_name='Systolic Blood Pressure Abnormal',blank=True,null=True)
    sys_bld_notes = models.TextField(max_length=250,blank=True,null=True,verbose_name='Systolic Blood Pressure Notes')
 
    dias_bld_pressure = models.FloatField( help_text="A patient's diastolic blood pressure measurement (taken with a manual cuff in either a sitting or standing position)",blank=True,null=True,verbose_name='Diastolic Blood Pressure(mmHg)') 
    dias_bld_abnormal= models.BooleanField(verbose_name=' Diastolic Blood Pressure Abnormal',blank=True,null=True)
    dias_bld_notes =models.TextField(max_length=250,blank=True,null=True,verbose_name="Diastolic Blood Pressure Notes")
#body position
    body_position = models.CharField(max_length=50, help_text="Body position for procedure or blood pressure measurement",blank=True,null=True,verbose_name='Body Position',choices=position)
    body_position_notes = models.TextField(max_length=250,blank=True,null=True,verbose_name='Body Position Notes')

    pulse = models.FloatField( help_text="Patient pulse rate, as measured with a peripheral oximeter.",blank=True,null=True,verbose_name='Pulse(beats/min)') 
    pulse_abnormal = models.BooleanField(verbose_name='Pulse Abnormal',blank=True,null=True)

    bld_oxy_saturation = models.FloatField( help_text="Arterial blood oxygen saturation, as measured from a peripheral oximeter",blank=True,null=True,verbose_name='Arterial Blood Oxygen Saturation(pulse oximeter)(1%)') 

    bld_oxy_saturation_abnormal = models.BooleanField(blank=True,null=True,verbose_name='Arterial Blood Oxygen Saturation Abnormal') 
    fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING, null=True)
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING, null=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    date_modified = models.DateTimeField(default=timezone.now)


# class fees_information(models.Model):
#     class Meta:
#         verbose_name_plural = "Fees Information"

    
#     reg_fees = models.FloatField( help_text="Enter your registration fees",default=0.0,null=True,verbose_name=' Registration Fee')
#     doc_name = models.CharField(max_length=100,help_text="Enter Doctor's name",null=True,verbose_name='Doctor Name')
#     Cons_fee = models.FloatField(help_text="Enter your consultation fees",default=0.0,null=True,verbose_name=' Consultation Fee')

#     Cons_fee_notes = models.TextField(blank=True,null=True,verbose_name=' Consultation Fee notes')
#     fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING, null=True)
#     fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING, null=True)
#     date_created = models.DateTimeField(auto_now_add=True, null=True)
#     date_modified = models.DateTimeField(default=timezone.now)


class History_examination(models.Model):

    class Meta:
        verbose_name_plural = "History and Examinations"

    list_units =(('Hours','Hours'),('Days','Days'),('Weeks','Weeks'),('Months','Months'),('Years','Years'))
    list_smoking = (('Unknown if ever smoked','Unknown if ever smoked'),('Current every day smoker','Current every day smoker'),('Former smoker','Former smoker'),('Current light tobacco smoker','Current light tobacco smoker'),('Current heavy tobacco smoker','Current heavy tobacco smoker'),('Smoker','Smoker'),('Current some day smoker','Current some day smoker'),('Never smoked','Never smoked'))

    Chief_complaint = models.CharField(max_length=250, blank=True,null=True,help_text='Enter the complaints',verbose_name='Chief Complaints')
    symptom_duration = models.IntegerField(help_text='The numeric value of the duration of the sign or symptom.',verbose_name='Sign/Symptoms duration',blank=True,null=True)
    units = models.CharField(max_length=50, blank=True,null=True,help_text='Units which correspond to the numeric value entered for a duration',verbose_name='Units',choices=list_units)

    present_illness  = models.TextField(max_length=250,blank=True,null=True,verbose_name='History of Present illness')
    Smoking_status = models.CharField(max_length=50, blank=True,null=True,help_text='Select smoking status',verbose_name='Smoking Status',choices=list_smoking)
    consultation_images = models.ImageField(help_text='Upload Consultation image',verbose_name='Consultation Images', blank=True,null=True)

    fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING, null=True)
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING, null=True)
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    #consultation_video =

class vitals(models.Model):
    class Meta:
        verbose_name_plural ="vitals"

    temperature = models.FloatField(help_text="Enter temperature in fahrenheit",default=0.0,blank=True,null=True,verbose_name='Temperature')
    temperature_abnormal = models.BooleanField(blank=True,null=True,verbose_name='Temperature Abnormal') 
    temperature_notes = models.TextField(max_length=250,blank=True,null=True,verbose_name='Temperature Notes')

    Res_rate= models.FloatField(help_text="Enter the respiratory rate",default=0.0,blank=True,null=True,verbose_name='Respiratory Rate')
    Res_rate_abnormal = models.BooleanField(blank=True,null=True,verbose_name='Respiratory Rate Abnormal') 
    Res_notes= models.TextField(max_length=250,blank=True,null=True,verbose_name='Respiratory Notes')

    fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING, null=True)
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING, null=True)
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

class scheduling(models.Model):
    class Meta:
        verbose_name_plural ="Schedlings"

    date_scheduled = models.DateTimeField(verbose_name='Date Scheduled',null=True,help_text='Enter the scheduling date')
    time_duration = models.DurationField( blank=True,null=True,verbose_name='Duration',help_text='Enter the time for scheduling ')


    fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING, null=True)
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING, null=True)
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)


class treatment_details(models.Model):
    class Meta:
        verbose_name_plural ="Treatment details"
    
    fk_treatment = models.ForeignKey(treatment,help_text="Select the treatment",on_delete=models.CASCADE,null=True,verbose_name='Treatment')

    treatment_notes = models.TextField(max_length=250, blank=True,null=True,verbose_name='Treatment Notes')


    fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING, null=True)
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING, null=True)
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    
# @receiver(post_save, sender = nutritional_value)
# def BMI_calculate(sender, instance, **kwargs):
#         dbl_base = instance.height/100
#         dbl_BMI = instance.weight/(dbl_base*dbl_base)
#         instance.BMI = round(dbl_BMI,2) # instance.weight/( instance.height/100* instance.height/100) # dbl_BMI# instance.weight/(dbl_base*dbl_base)
#         print(instance.BMI)
#         instance.save()    
        