from django.db import models
from django.contrib.auth.models import User

class Category():

	EARNED_LEAVE = 'EL'
	SICK_LEAVE = 'SL'
	COMPENSATORY_LEAVE = 'CL'
	LEAVE_WITHOUT_PAY = 'LWP'    
	HOLIDAY_WORKED = 'HW'
	PAID_TIME_OFF = 'PTO'
	WORK_FROM_HOME = 'WFH'
	PATERNITY_LEAVE = 'PL'
	BEREAVEMENT_LEAVE = 'BL'
	WEDDING_DAY_LEAVE = 'WDL'
    
    
	LEAVE_CHOICES = (
		(EARNED_LEAVE, 'Earned Leave'),
		(SICK_LEAVE, 'Sick Leave'),
		(COMPENSATORY_LEAVE, 'Compensatroy Leave'),
		(LEAVE_WITHOUT_PAY, 'Leave Without Pay '),
		(HOLIDAY_WORKED, 'Holiday Worked'),
		(PAID_TIME_OFF, 'Paid Time Off'),
		(WORK_FROM_HOME, 'Work From Home'),
		(PATERNITY_LEAVE, 'Paternity Leave'),
		(BEREAVEMENT_LEAVE, 'Bereavement Leave'),
		(WEDDING_DAY_LEAVE, 'Wedding Day Leave'),
	)




class Employee(models.Model):
		
		"""
        Employee profile get it from Active directory
        """

		INACTIVE = 0
		ACTIVE = 1
		EMPLOYEE_STATUS = (
		                (INACTIVE, 'Inactive'),
		                (ACTIVE, 'Active'),
		)

		MALE = 0
		FEMALE = 1 
		GENDER_CHOICES = (
		                (MALE, 'Male'),
		                (FEMALE, 'Female'),
		)

		user = models.ForeignKey(User, unique=True)
		emp_id = models.IntegerField(primary_key=True, unique=True, help_text='Employee id unique')
		fullname = models.CharField(max_length = 100, blank=True, help_text='Employee Full Name')
		gender = models.IntegerField(choices=GENDER_CHOICES, blank=True, help_text="Employee gender")
		email_id = models.EmailField(max_length=70, blank=True, null= True)
		joining_date = models.DateField(blank=True, editable=True, null=True, help_text="Employee joining date in SmarTek")
		status = models.IntegerField(default=1, choices=EMPLOYEE_STATUS)
		designation = models.CharField(max_length=100, null=True, blank=True, help_text="Employee current designation")

		manager = models.ForeignKey("Employee", limit_choices_to={'is_manager': True}, null=True, blank=True, related_name="user_is_manager", help_text="Manager ID")        
		location = models.CharField(max_length = 100, blank=True, null=True, help_text="Location name")
		is_manager = models.BooleanField(default = 0, help_text="Check whether employee is Manager")

		is_admin = models.BooleanField(default = 0, help_text="Check whether employee is administrator.")
		created_on = models.DateTimeField(auto_now_add=True, help_text="Auto generated by system")
		modified_on = models.DateTimeField(auto_now=True, help_text="Auto generated by system")

		def __unicode__(self):
		        return self.fullname if self.fullname else ''

		class Meta:
		        verbose_name = 'Employee'
		        verbose_name_plural = 'Employee'


class Leave(models.Model):
	
	STATUS = (
		(0, 'Pending'),
		(1, 'Approved'),
		(2, 'Cancel'),		
	)

	to_date = models.DateField()
	from_date = models.DateField()	
	category = models.CharField(max_length=4, choices=Category.LEAVE_CHOICES)
	no_of_days = models.IntegerField()
	request_date = models.DateField(auto_now_add=True)
	contact_details = models.TextField(null=True, blank=True)
	reason = models.TextField(null=True, blank=True)
	employee = models.ForeignKey(Employee)
	status = models.IntegerField(choices=STATUS)

	def __unicode__(self):
		return str(self.employee)+"__"+self.category


class LeaveBalance(models.Model):

	employee = models.ForeignKey(Employee)
	category = models.CharField(max_length=4, choices=Category.LEAVE_CHOICES)
	balance = models.IntegerField(default=0)
	availed = models.IntegerField(default=0)
	carry_forward = models.IntegerField(default=0)

	class Meta:
		unique_together = ('employee', 'category')
	
	def __unicode__(self):
		return str(self.employee)+"__"+self.category+"__"+str(self.balance)



	
