from django.db import models
from datetime import datetime
from baseapp.models import patient,employee, Master_GST,Zone_System
from django.utils import timezone 
from django.core.exceptions import ValidationError
from django.db.models.signals import pre_save, post_save

list_unit  = (('Capsule', 'Capsule'),('Tablet', 'Tablet'),('ml', 'ml'),('mg', 'mg'),('IU', 'IU'),('Drop', 'Drop'),('Tablespoon', 'Tablespoon'),('Teaspoon', 'Teaspoon'),('Unit(s)', 'Unit(s)'),('Others', 'Others'))
def validate_InventoryUnit(str_value):  
    if str_value ==0:        raise ValidationError(u'0 is not accpeted')



# class med_unit(models.Model):


#     unit_type = models.CharField(max_length=20,blank=True,verbose_name='Unit Type',null=True,help_text='Enter a type of the unit')

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

#     def __str__(self):
#         return self.unit_type 
    
class medic_inhouse(models.Model):


    drug_name = models.CharField(max_length=50,null=True,verbose_name='Drug Name',help_text='Enter the drug name')
    drug_tamil = models.CharField(max_length=75,help_text='மருந்தின் பெயரை உள்ளிடவும்',blank=True,null=True,verbose_name='மருந்து பெயர்')
    drug_details = models.TextField(max_length=150,help_text='Enter the drug in details',blank=True,null=True,verbose_name='Details')
    fk_system = models.ForeignKey(Zone_System,on_delete=models.DO_NOTHING,null=True,verbose_name='System',help_text='Enter the drug system')
    Preparations_type = models.CharField(max_length=50,null=True,verbose_name='Preparation Type',help_text='Enter the drug preparation')
    group  = models.CharField(max_length=50,null=True, blank= True,  verbose_name='Drug Group',help_text='Enter the group (optional)')
    source_type  = models.CharField(max_length=50,null=True, blank= True,  verbose_name='Source Type',help_text='the details of how it is made')
    packaging = models.CharField(max_length=25,help_text='Packing Size',blank=True,null=True,verbose_name='Packing Size')
    unit_amount =  models.FloatField( help_text="Unit Price ",blank=True,null=True,verbose_name='Unit Price')
    fk_GST = models.ForeignKey(Master_GST, on_delete=models.DO_NOTHING,null=True,blank=True)     
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING)
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
        return str(self.id) +  ' - ' +  str(self.drug_name) +  ' - ' + str(self.fk_system  ) +  ' - ' + str(self.packaging  ) +  ' - ' + str(self.Preparations_type  )



class medications(models.Model):
    class Meta:
        verbose_name_plural ="Medications"



    list_frequency =(('Immediately','Immediately'),('Once a day','Once a day'),('Twice a day','Twice a day'),('Thrice a day','Thrice a day'),('Four times a day','Four times a day'),('Every Hour', 'Every Hour'),('Every 2 hours', 'Every 2 hours'),('Every 3 hours', 'Every 3 hours'),('Every 4 hours', 'Every 4 hours'), ('Every 6 hours', 'Every 6 hours'),('Every 8 hours', 'Every 8 hours'),('Every 12 hours', 'Every 12 hours'),('On alternate days', 'On alternate days'),('Once a week', 'Once a week'),('Twice a week', 'Twice a week'),('  Once a month', '  Once a month'),('Six days a week', 'Six days a week'),('Every 2 weeks', 'Every 2 weeks'),('Every 3 weeks', 'Every 3 weeks'),('Five times a day', 'Five times a day'),('Four days a week', 'Four days a week'),('Five days a week', 'Five days a week'))
    list_route =(('Intramuscular','Intramuscular'),('Nasal', 'Nasal'),('Topical', 'Topical'),('Intraosseous','Intraosseous'),('Intrathecalan','Intrathecalan'),('Intraperitoneal', 'Intraperitoneal'),('Intradermal','Intradermal'),('Nasogastric','Nasogastric'),('Sub Lingual', 'Sub Lingual'),('Per Rectum','Per Rectum'),('Sub Cutaneous', 'Sub Cutaneous'),('Per Vaginal', 'Per Vaginal'),('Oral', 'Oral'),('Intravenous', 'Intravenous'),('Inhalation','Inhalation'))
    list_units = (('Days', 'Days'),('Weeks', 'Weeks'),('Months', 'Months'))

    fk_med =  models.ForeignKey(medic_inhouse,help_text='Select the medicine - available in stock',on_delete=models.DO_NOTHING,null=True, blank=True,verbose_name='Medicine')
    drug_name = models.CharField(max_length=50,null=True, blank=True,verbose_name=' Drug Name (optional)- apart from listed medicine',help_text='Enter the drug name')
    accept =  models.BooleanField(blank=True,null=True,verbose_name="Accept")

    dose = models.IntegerField(help_text='Enter the unit of dosage',verbose_name='Dose',blank=True,null=True)
    units = models.CharField(max_length=20,blank=True,null=True,verbose_name=' Units',help_text='Select unit for dosage ',choices=list_unit)

    frequency = models.CharField(max_length=20,blank=True,null=True,verbose_name='Frequency',help_text='Select frequency',choices=list_frequency)
    route = models.CharField(max_length=20,blank=True,null=True,verbose_name='Route',help_text='Select route of drug ',choices=list_route)
    starting_date = models.DateField(help_text='Enter start date', default=datetime.today,verbose_name=' Start Date')
    duration = models.IntegerField(help_text='Duration of dosage',verbose_name='Duration',blank=True,null=True)

    days_unit =  models.CharField(max_length=20,blank=True,null=True,verbose_name='Maximum duration',help_text='Select duration of dose ',choices=list_units)
    
    Ttl_qunaty = models.FloatField(help_text='Total qunatity of dosage',verbose_name='Total Quantity',default=0.0,blank=True,null=True)
    ttl_units = models.CharField(max_length=20,blank=True,null=True,verbose_name='Units',help_text='Select unit for total quantity',choices=list_unit)
    additional_notes = models.TextField(max_length=250,blank=True,null=True,verbose_name='Additional Notes')

    fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING)
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING)
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    def __str__(self):
        return self.drug_name + ' - ' + str(self.units)
    
class inventory_entry(models.Model):
    list_type = (('Billed', 'Billed'), ('Goods Recieved', 'Goods Recieved'),('Damaged', 'Damaged'),('Expired', 'Expired'))

    fk_med =  models.ForeignKey(medic_inhouse,help_text='Select the medicine',on_delete=models.DO_NOTHING,null=True,verbose_name='Medicine')
    #units = models.CharField(max_length=20,null=True,verbose_name=' Units',help_text='Select unit for dosage ',choices=list_unit)
    dosage_unit = models.FloatField(verbose_name='Dosage Unit',help_text='Enter the dosage unit', validators=[validate_InventoryUnit])
    entry_type = models.CharField(max_length=20,verbose_name='Entry Type',help_text='Select entry type from the list',choices=list_type, default='Goods Recieved' )
    entry_details = models.TextField(max_length=250,help_text='Entry details like lot # or Batch # can be mentioned here.',blank=True,null=True,verbose_name='Details')
    #fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING,null=True,blank=True) 
    #fk_patient_medication = models.ForeignKey(medications,on_delete=models.DO_NOTHING,null=True,blank=True)
    medications
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING)
    fk_patient = models.ForeignKey(patient, on_delete=models.DO_NOTHING, null=True, blank=True)
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    
    def __str__(self):
        return str(self.fk_med)    