from django.db import models
from django.contrib.auth.models import User
from phonenumber_field.modelfields import PhoneNumberField
from django.utils import timezone
from django_enum import EnumField
from django.utils.text import slugify
import datetime

# Abstract Model Classes
class TimeMixin(models.Model):

    created = models.DateTimeField(default=timezone.now)
    last_modified = models.DateTimeField(default=timezone.now)
    created_by = models.ForeignKey
    last_modified_BY = models.ForeignKey

    class Meta:
        abstract = True

class IdMixin(models.Model):
    slug = models.SlugField()

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        if self.slug is None:
            self.slug = slugify(datetime.datetime.now().time())
        super(IdMixin, self).save(*args, **kwargs)

# Concrete Classes
class Contract(IdMixin, TimeMixin):
    class ContractTypeEnum(models.TextChoices):
        FIRM_FIX_PRICED = "FFP", "FIRM_FIX_PRICED"
        MAX_NUM_CONRACT_TYPES = "MAX", "MAX_NUM_CONTRACT_TYPES"

    contract_type = EnumField(ContractTypeEnum)

    name = models.CharField(max_length=100)
    proposed_amount = models.FloatField(default=0.0)
    baseline_amount = models.FloatField(default=0.0)
    funded_amount = models.FloatField(default=0.0)

    baseline_start = models.DateField(null=True, blank=True, default=None)
    baseline_end = models.DateField(null=True, blank=True, default=None)
    linkedContracts = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True)

    # TODO: make calc ev func

class ChargeNumber(IdMixin, TimeMixin):
    class ChargeNumberTypeEnum(models.TextChoices):
        LEVEL_OF_EFFORT = "LOE", "LEVEL_OF_EFFORT"
        QBD = "QBD", "QBD"
        ZERO_ONE_HUNDRED = "0_100", "ZERO_ONE_HUNDRED"
        FIFTY_FIFTY = "50_50", "FIFTY_FIFTY"
        MAX_NUM_TASK_TYPES = "MAX", "MAX_NUM_TASK_TYPES"

    charge_number_type = EnumField(ChargeNumberTypeEnum)

    contract = models.ForeignKey(Contract, on_delete=models.CASCADE)
    amount = models.FloatField(default=0.0)
    percent_complete = models.FloatField(default=0.0)
    # TODO: add validator to make sure the range is 0.0 - 100
    start_date = models.DateField(null=True, blank=True, default = None)
    end_date = models.DateField(null=True, blank=True, default = None)

class AddressModel(models.Model):
    address_1 = models.CharField(max_length=128)
    address_2 = models.CharField(max_length=128, blank=True)

    city = models.CharField(max_length=64)
    state = models.CharField(max_length=2)
    zip_code = models.CharField(max_length=5)

class Employee(IdMixin, TimeMixin):
    manager = models.ForeignKey("self", on_delete=models.CASCADE, related_name="manager_employee")
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    primaryAddress = models.ForeignKey(AddressModel, on_delete=models.CASCADE, related_name="primary_address_employee")
    workAddress = models.ForeignKey(AddressModel, on_delete=models.CASCADE, related_name="work_address_employee")
    phoneNumber = PhoneNumberField(null=False, blank=False, unique=True)
    slary= models.FloatField(default=0.0)

    # TODO: get hourly salary
    # TODO: roles, jpbTitles



class TimeCard(IdMixin, TimeMixin):
    startDate = models.DateField(null=True, default = None)
    endDate = models.DateField(null=True, default = None)
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name="employee")
    approvedBy = models.ForeignKey(Employee, on_delete=models.CASCADE, null=True, related_name="approved_by")
    approvedDate = models.DateField(null=True, default=None)
    approvalComment = models.TextField(max_length=256, default="")
    # TODO: do the is approved comment

class TimeCardCell(IdMixin, TimeMixin):
    timeCard = models.ForeignKey(TimeCard, on_delete=models.CASCADE)
    date = models.DateField(null=True, default = None)
    hour = models.FloatField(default = 0.0)





