from django.db import models
from datetime import datetime
from django.contrib.auth.models import User, Group, GroupManager #Group,AbstractUser,
from ckeditor.fields import RichTextField
from colorfield.fields import ColorField
from django.utils import timezone 
from django.core.exceptions import ValidationError
from .internal import s3_service
from django.db.models.signals import pre_save, post_save
# Create models here.

# base class for users

def validate_mobilenum(value):
    if len(str(value)) != 10 :        raise ValidationError(u'Only 10 digit Mobile Numbers are accepted')
def validate_zipcode(value):
    if len(str(value)) !=  6:        raise ValidationError(u'Only 10 digit Mobile Numbers are accepted')    
def validate_file_extension(value):
     pass  
    # if not (value.file.content_type == 'image/jpg' or value.file.content_type == 'image/jpeg' or value.file.content_type == 'image/png'):
    #     raise ValidationError(u'Please choose only Image file jpg or jpeg')

def s3_file(sender, instance, created,*args, **kwargs):
        str_file = str(instance.profile_pic)
        s3_service.object_upload(str_file,str_file)



     
class intchoice_YN(models.IntegerChoices):
       Yes = 1
       No = 2
class intchoice_OC(models.IntegerChoices):
       Open = 1
       Closed = 2
#list_duration = (('Second'),('Minute'),('Hour'),('Day'),('Week'),('Month'),('Year'))
class intchoice_duration(models.IntegerChoices):
        Second = 1
        Minute  = 2
        Hour =3
        Day = 4
        Week = 5
        Month = 6
        Year =  7

class intchoice_gender(models.IntegerChoices):
     male = 1
     female = 2
     Trangender = 3    

class intchoice_bootstrap_inidcator(models.IntegerChoices):
     success = 1
     Warning = 2
     info = 3
     danger = 4    
     gray = 5 

class gender(models.Model):
     
    gender_name =  models.CharField( null=True,max_length=75, help_text='Enter a gender',verbose_name='Gender')
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    def __str__(self):
        return self.gender_name
    
class User_Group(models.Model):
    #Group
    class Meta:
            verbose_name = "User Group"
    name = models.CharField(max_length=25, help_text='Name of the Group', verbose_name='Name of the Group')  
    short_notes = models.CharField(max_length=75, help_text='short description for Group', verbose_name='Short Notes')  
    description = models.TextField(max_length=200, help_text='Enter Description for Group', verbose_name='Detailed notes', 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 self.short_notes    

class country_state(models.Model):
    class Meta:
            verbose_name = "Country State"
            verbose_name_plural  = "Country States"

    district = models.CharField(max_length=75, help_text='Enter District', verbose_name='District')  
    state_code = models.CharField(max_length=25, help_text='enter state code eg., TN',verbose_name='State Code', default='TN')
    state_name = models.CharField(max_length=25, help_text='enter State name',verbose_name='State', default='Tamil Nadu') 
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    def __str__(self):
        return self.state_name


class tags(models.Model):
    class Meta:
            verbose_name = "Tag"
            verbose_name_plural  = "Tags"
    tag_name = models.CharField(max_length=75, help_text='Enter your tag name',default='',verbose_name='Tag Name') 
    tag_description = models.TextField(max_length=250,default='',verbose_name='Tag Description',help_text='Enter a tag description')
    tag_color = ColorField(verbose_name='Tag Color',help_text='Select your tag colour', default='')
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    def __str__(self):
        return self.tag_name + ' - ' + self.tag_color
    
class base_contact(models.Model):
    class Meta:
        abstract = True
    phone_number = models.IntegerField( help_text='Primary Mobile Number',blank=True,null=True,verbose_name='Phone Number') 
    email = models.EmailField( help_text='Enter your Email',default='berlinsuderson73@gmail.com',verbose_name='Email') 
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True) 

     
class base_user(base_contact): #phone number and email will ge get from base_contact
    class Meta:
        abstract = True

    list_Gender = (('Male', 'Male'),('Female', 'Female'),('Trasgender', 'Trasgender'))
    first_name = models.CharField(max_length=50, help_text='Enter your first name',default='',verbose_name='First Name')
    last_name = models.CharField(max_length=50, help_text='Enter your last name',default='',verbose_name='Last Name') 
    full_name = models.CharField(max_length=75, help_text='full name as per record',default='',verbose_name='Full Name')
    profile_pic = models.ImageField(upload_to='static\members\pic', blank=True, null=True,verbose_name='Profile Pic', validators=[validate_file_extension]) 
    phone_number2 = models.BigIntegerField( help_text='Secondary/Alternative Phone Number',default='',verbose_name="Mobile Number")
    DoB = models.DateField(help_text='Enter your DoB',default=datetime.today,verbose_name='DoB')
    age =models.IntegerField(help_text='Enter your age',default='',verbose_name='Age')
    gender = models.IntegerField(choices=intchoice_gender.choices, help_text='Select your gender',default='Female',verbose_name='Gender')
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)


class base_address(models.Model):
    class Meta:
        abstract = True
        
    address1 =models.CharField(max_length=50, help_text='Door No,Building No',default='',verbose_name='Address')
    address2 =models.CharField(max_length=50, help_text='Street, Area, Locality',default='',verbose_name='Address 1')
    state = models.ForeignKey(country_state, max_length=25, help_text='Select your state',default='', on_delete=models.CASCADE,verbose_name='State')
    zipcode = models.IntegerField(help_text='enter your zipcode',default='',verbose_name='Zipcode')



class Zone_System(models.Model):
     class Meta:
            verbose_name = "Zone System"
            verbose_name_plural  = "Zone System"
     System = models.CharField(max_length=75,verbose_name='Zone System',help_text='Enter Zone System')
     Description = models.CharField(max_length=125,verbose_name='System Description',help_text='Enter Description for System')
     date_modified = models.DateTimeField(default=timezone.now)
     date_created = models.DateTimeField(auto_now_add=True, null=True)

     def __str__(self):
        return str(self.System)
class Zone_Type(models.Model):
     class Meta:
            verbose_name = "Zone Type"
            verbose_name_plural  = "Zone Type"
     Ztype = models.CharField(max_length=125,verbose_name='Zone Type',help_text='Enter Zone Type')
     date_modified = models.DateTimeField(default=timezone.now)
     date_created = models.DateTimeField(auto_now_add=True, null=True)
     def __str__(self):
        return str(self.Ztype)

class District_State(models.Model):
     class Meta:
            verbose_name = "District & State"
            verbose_name_plural  = "District & State"
     District   = models.CharField(max_length=125,verbose_name='District Name',help_text='Enter District Name',null=True)
     State      = models.CharField(max_length=125,verbose_name='State Name',help_text='Enter State Name',null=True, default='Tamil Nadu')
     date_modified = models.DateTimeField(default=timezone.now)
     date_created = models.DateTimeField(auto_now_add=True, null=True)
     def __str__(self):
        return self.District  +  '-'  +  str(self.State)
     
class ID_type(models.Model):
     
     list_type =  (('Alphabet','Alphabet'),('Numeric','Numeric'),('Alphanumeric','Alphanumeric') )


     name =  models.CharField(max_length=50,verbose_name='Proof Name',help_text='Eg Aadhaar card,PAN card etc...',blank=True,null=True)
     shrt_txt = models.CharField(max_length=125,verbose_name='Short Description',help_text='Enter a short description',null=True,blank=True)
     field_type = models.CharField(max_length=75,verbose_name='Proof type',help_text='Eg Aadhaar card conatins only Numeric value',blank=True,null=True,choices=list_type)
     regex_validation = models.CharField(max_length=125,verbose_name='Regex Validation',help_text='Enter a regex validation',null=True,blank=True)
     No_of_character = models.IntegerField(verbose_name='Number of Characters',help_text='Enter the number of chatacers for proof',null=True,blank=True)

     def __str__(self):
         return self.name
    

class Zone(models.Model):
     class Meta:
            verbose_name = "Zone"
            verbose_name_plural  = "Zone"
     center = models.CharField(max_length=125,verbose_name='Center',help_text='Enter the center',null=True)
        # District and Zone Field will be removed soon, it will be replaced by the drop down menu to reduce manual mistakes
    #  District = models.CharField(max_length=125,help_text='Enter the district - District Field will be removed soon, it will be replaced by the drop down menu to reduce manual mistakes ',null=True,verbose_name='District')
    #  Zone = models.CharField(max_length=125,help_text='Enter the Zone - This Field will be removed soon ',null=True,verbose_name='Zone')

     fk_System = models.ForeignKey(Zone_System,help_text='Select a System',verbose_name='System', on_delete=models.DO_NOTHING,related_name='%(class)s_fk_System')
     fk_Type = models.ForeignKey(Zone_Type,help_text='Select a Type',verbose_name='Type', on_delete=models.DO_NOTHING,related_name='%(class)s_fk_Type')
     fk_District = models.ForeignKey(District_State,help_text='Select a District',verbose_name='District', on_delete=models.DO_NOTHING,related_name='%(class)s_fk_District')

     id_employee = models.IntegerField(blank=True,null=True)
     date_modified = models.DateTimeField(default=timezone.now)
     date_created = models.DateTimeField(auto_now_add=True, null=True)

     def __str__(self):
        return str(self.id) +  '-'  + self.center +  '-' +  str(self.fk_System.System) +  '-' + str(self.fk_Type.Ztype) +  '-'  + str(self.fk_District.District)
     
class employee(models.Model):

    list_title     = (('Mr', 'Mr'),('Mrs', 'Mrs'),('Miss', 'Miss'),('Ms', 'Ms'),('Dr', 'Dr'))
    list_Gender = (('Male', 'Male'),('Female', 'Female'),('Trasgender', 'Trasgender'))

    profile_pic = models.ImageField(upload_to='static\employee\profile', blank=True, null=True,verbose_name='Profile Pic',  help_text='You can take a photo and upload here or you can use camera to upload photo in profile page',  validators=[validate_file_extension])
    Title = models.CharField(max_length=50,verbose_name='Title',choices=list_title)
    employee_name = models.CharField(max_length=50, help_text='Name of the employee',verbose_name='Employee Name',null=True)
    last_name = models.CharField(blank=True, null=True,max_length=75, help_text='Employee initial',verbose_name='Last Name/Initial')
    fk_gender = models.ForeignKey(gender,help_text='Select a gender',verbose_name='Gender', on_delete=models.DO_NOTHING)
    #gender = models.CharField( null=True,max_length=25,choices=list_Gender, help_text='Select your gender',verbose_name='Gender')
    DoB = models.DateField( null=True,default=timezone.now,help_text='Enter your date of birth',verbose_name='DoB')
    proof = models.ForeignKey(ID_type,on_delete=models.DO_NOTHING,help_text='Select a proof',verbose_name='Proof Name',null=True,blank=True)
    proof_number = models.CharField(max_length=75,help_text='Enter your ID Proof Number for patient (Optional)',null=True, blank=True,verbose_name='ID Proof Number')    
# Location Information
    Address1 = models.CharField(max_length=250,help_text='Enter your house or flat number',null=True,verbose_name='House/Flat number')
    locality = models.CharField(max_length=50,help_text='Area of living',blank=True,null=True,verbose_name='Locality/Sector')
    city = models.CharField(max_length=50,help_text='Enter your location (city or village name)',null=True,verbose_name='City/Village')
    Pincode = models.IntegerField(help_text='Enter your pincode',null=True,verbose_name='Pincode',validators=[validate_zipcode])
    fk_dist = models.ForeignKey(District_State, on_delete=models.DO_NOTHING,help_text='Enter Your district ', blank= True, null=True,verbose_name='District')
    #State = models.CharField(max_length=50,help_text='Enter your state ',null=True,verbose_name='State',default='Tamil Nadu')
# contact Information    

    education_qualification = models.CharField(max_length=75,help_text='Enter your qualification',null=True,verbose_name='Educational Qualification')
    email = models.EmailField( help_text='Enter your email id',verbose_name='Email',blank=True,null=True)
    phone_number = models.BigIntegerField( help_text='Enter your primary phone number',blank=True,null=True,verbose_name='Phone Number',validators=[validate_mobilenum]) 
    alt_phone_number = models.BigIntegerField( help_text='Enter your alternative phone number',blank=True,null=True,verbose_name='Alternative Phone Number',validators=[validate_mobilenum]) 
# connect username/login userid
    str_username = models.CharField(max_length=50,verbose_name='User Name',help_text='User Name used for employee login (it will be automatically created by application)')
            # temproary password - for password reset or newly added user (bulk uploads) this term will be used
    temp_password = models.CharField(max_length=50,help_text='Enter Temproary password for reset or Newly Created Users',null=True, blank=True, verbose_name='Temproary password')
    fk_group = models.ForeignKey(User_Group,help_text="Select a group",verbose_name='User Group', on_delete=models.DO_NOTHING)
    fk_zone = models.ForeignKey(Zone,help_text="Select a zone", 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.str_username
post_save.connect(s3_file,sender=employee)



    
     
class patient(models.Model):

    list_title     = (('Mr', 'Mr'),('Mrs', 'Mrs'),('Miss', 'Miss'),('Ms', 'Ms'),('Dr', 'Dr'))
    list_Gender = (('Male', 'Male'),('Female', 'Female'),('Trasgender', 'Trasgender'))

    profile_pic = models.ImageField(upload_to='static\patient\profile', blank=True, null=True,verbose_name='Profile Pic',  help_text='You can take a photo and upload here or you can use camera to upload photo in profile page', validators=[validate_file_extension])
    Title = models.CharField(max_length=50,verbose_name='Title',choices=list_title,null=True)
    Patient_name = models.CharField(max_length=70, help_text='Enter the full name',verbose_name='Patient Name',null=True)
    # first_name = models.CharField(max_length=50, help_text='Enter the first name',verbose_name='First Name',null=True)
    last_name = models.CharField(max_length=75, help_text='Enter the last name ',verbose_name='Last Name/Initial',blank=True,null=True)
    fk_gender = models.ForeignKey(gender,help_text='Select gender',verbose_name='Gender', on_delete=models.DO_NOTHING)

    #gender = models.CharField(max_length=25,choices=list_Gender, help_text='Select your gender',verbose_name='Gender',null=True)
    DoB = models.DateField(help_text='Enter date of birth',default=timezone.now,verbose_name='DoB',null=True)
# Location Information
    
    Address1 = models.CharField(max_length=50,help_text='Enter house or flat number', blank=True,null=True,verbose_name='House/Flat number')
    locality = models.CharField(max_length=50,help_text='Area of living ',blank=True,null=True,verbose_name='Locality/Sector')
    city = models.CharField(max_length=50,help_text='Enter the location (city or village name)', blank=True,null=True,verbose_name='City/Village')
    Pincode = models.IntegerField(help_text='Enter the pincode',null=True, blank=True,verbose_name='Pincode',validators=[validate_zipcode])
    fk_dist = models.ForeignKey(District_State, on_delete=models.DO_NOTHING,help_text='Enter Your district ', blank= True, null=True,verbose_name='District')
    #State = models.CharField(max_length=50,help_text='Enter the state ',null=True,verbose_name='State',default='Tamil Nadu')
    proof = models.ForeignKey(ID_type,on_delete=models.DO_NOTHING,help_text='Select a proof',verbose_name='Proof Name',null=True,blank=True)
    proof_number = models.CharField(max_length=75,help_text='Enter your ID Proof Number for patient (Optional)',null=True, blank=True,verbose_name='ID Proof Number')
# contact Information    
    email = models.EmailField( help_text='Enter email id',verbose_name='Email',null=True, blank=True)
    phone_number = models.BigIntegerField( help_text='Enter primary phone number',null=True, blank=True,verbose_name='Phone Number',validators=[validate_mobilenum])
    alt_phone_number = models.BigIntegerField( help_text='Enter alternative phone number',blank=True,null=True,verbose_name='Alternative Phone Number',validators=[validate_mobilenum]) 
    fk_zone = models.ForeignKey(Zone,help_text="Select a zone", on_delete=models.DO_NOTHING, null=True, verbose_name='Zone')

    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)
post_save.connect(s3_file,sender=patient)

class disease(models.Model):
     class Meta:
        verbose_name_plural ="Disease"
     
     disease_name =  models.CharField(max_length=75,verbose_name='Disease Name',help_text='Enter the disease name',null=True)
     disease_name_tamil = models.CharField(max_length=75,verbose_name='நோய் பெயர்',help_text='நோயின் பெயரை உள்ளிடவும்',blank=True,null=True)
     english_version =  models.CharField(max_length=75,verbose_name='Disease Name - English Version',help_text='Enter the disease name as per english version',null=True)
     fk_system = models.ForeignKey(Zone_System,on_delete=models.DO_NOTHING, null=True,verbose_name='System',help_text='Enter the System')
     subsystem =  models.CharField(max_length=75,verbose_name='Sub System',help_text='Sub System details',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)

     def __str__(self):
          return self.disease_name + ' - ' + self.disease_name_tamil

class task(models.Model):
    class Meta:
        verbose_name_plural ="Tasks"

     #need to check list_task

    list_task = (('Primary','Primary'),('Success','Completed'),('Warning','Important'),('Info','Self-Assigned'),('Danger','Urgent'),('Dark','Missed'))

    task_type = models.CharField(max_length=50,help_text='Select a task type',verbose_name='Task type',choices=list_task, null=True)
    task_details  = RichTextField(blank=True,help_text='Enter the task in details',verbose_name='Task details',null=True)
    fk_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING, null=True,related_name='%(class)s_fk_employee')
    applyto_employee = models.ForeignKey(employee, on_delete=models.DO_NOTHING, null=True,verbose_name='Apply to employee',related_name='%(class)s_apply_to_employee')
    date_modified = models.DateTimeField(default=timezone.now,)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
          return self.task_type 


class treatment(models.Model):
    class Meta:
        verbose_name_plural ="Treatments"

    treatment_name =  models.CharField(max_length=50, help_text="Enter a Treatment Name",blank=True,null=True,verbose_name='Treatment Name')
    treatment_tamil_name =  models.CharField(max_length=50, help_text="சிகிச்சையின் பெயரை உள்ளிடவும்",blank=True,null=True,verbose_name='சிகிச்சையின் பெயர்')
    fk_system = models.ForeignKey(Zone_System,on_delete=models.DO_NOTHING, null=True,verbose_name='System',help_text='Enter the System')
    details  = models.TextField(max_length=250, blank=True,null=True,help_text='Write about treatment in details',verbose_name='Treatment details')
    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)

    def __str__(self):
          return self.treatment_name 


class Master_captcha(models.Model):
    class Meta:
        verbose_name_plural ="Master Captcha"     
        verbose_name ="MDM Captcha"     
    intext =  models.CharField(max_length=15, help_text="Captcha Character",blank=True,null=True,verbose_name='Captcha in Character field - for the background verification', unique=True)
    inimage  = models.FileField(blank=False,verbose_name='Image of Captcha', help_text='Captcha Image path will be shown here')
    date_modified = models.DateTimeField(default=timezone.now)
    date_created = models.DateTimeField(auto_now_add=True, null=True)       
    def __str__(self):
          return self.intext 

    

class Master_GST(models.Model):
    class Meta:
        verbose_name_plural ="Master GST"     
        verbose_name ="MDM GST"     
    gst_shorttext =  models.CharField(max_length=15, help_text="GST Short description",blank=True,null=True,verbose_name='GST Short description')
    gst_longtext =  models.CharField(max_length=150, help_text="GST Long description",blank=True,null=True,verbose_name='GST Long description')
    gst_value =  models.FloatField( help_text="Total GST % ",blank=True,null=True,verbose_name='Net GST %')
    cgst_value =  models.FloatField( help_text="Central or Integrated GST %",blank=True,null=True,verbose_name='CIGST %')
    sgst_value =  models.FloatField( help_text="State GST %",blank=True,null=True,verbose_name='SGST %')
    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)       
    def __str__(self):
          return self.gst_shorttext     
    


