from django.db import models

from authenticate.models import User
from institute.models import Institute

class Environment(models.Model):
    id = models.BigAutoField(primary_key=True, unique=True, db_index=True)
    name = models.CharField(max_length=46, unique=True)
    active_env = models.BooleanField(default=True)
    default_wage_factor = models.FloatField(default=0.12)
    default_tax_factor = models.FloatField(default=0.09)
    default_min_wage_fee = models.IntegerField(default=1000)
    default_max_institutes_per_user = models.IntegerField(default=5)
    default_max_collaborators_per_institute = models.IntegerField(default=10)
    default_max_question_banks_per_institute = models.IntegerField(default=200)
    default_max_questions_per_question_bank = models.IntegerField(default=5000)
    default_max_examinee_per_institute = models.IntegerField(default=1000)
    default_max_future_exam_per_institute = models.IntegerField(default=100)
    default_assign_examinee_to_exam = models.BooleanField(default=True)
    default_assign_public_link_to_exam = models.BooleanField(default=True)
    default_enable_institute_api_key = models.BooleanField(default=False)
    # default_apply_minimum_wage = models.BooleanField(default=True)
    # default_apply_wage_factor = models.BooleanField(default=True)
    initial_user_balance = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    modified_at = models.DateTimeField(auto_now=True, db_index=True)


    def _str_(self):
        return self.name

    @staticmethod
    def get_active_environment():
        # we do not store this active_env in a static variable because static variables will persist between APIs
        # means that if we edit the active env in Django admin,
        # it will take time until this static variable is cleared and the new value is read from database
        Environment.active_env_object, _ = Environment.objects.get_or_create(active_env=True)
        return Environment.active_env_object


class AbstractUserPlan(models.Model):
    id = models.BigAutoField(primary_key=True, unique=True, db_index=True)
    name = models.CharField(max_length=46, unique=True)
    duration = models.DurationField()
    price = models.IntegerField(null=True)
    actual_price = models.IntegerField()
    active = models.BooleanField()
    wage_factor = models.FloatField(null=True)
    tax_factor = models.FloatField(null=True)
    min_wage_fee = models.IntegerField(null=True)
    max_institutes_per_user = models.IntegerField(null=True)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    modified_at = models.DateTimeField(auto_now=True, db_index=True)

    def _str_(self):
        return self.name

    def get_final_price(self):
        if self.price:
            return self.price
        else:
            return self.actual_price


class AbstractInstitutePlan(models.Model):
    id = models.BigAutoField(primary_key=True, unique=True, db_index=True)
    name = models.CharField(max_length=46, unique=True)
    duration = models.DurationField()
    price = models.IntegerField(null=True)
    actual_price = models.IntegerField()
    active = models.BooleanField()
    max_collaborators_per_institute = models.IntegerField(null=True)
    max_question_banks_per_institute = models.IntegerField(null=True)
    max_questions_per_question_bank = models.IntegerField(null=True)
    max_examinee_per_institute = models.IntegerField(null=True)
    max_active_exam_per_institute = models.IntegerField(null=True)
    assign_examinee_to_exam = models.BooleanField(null=True)
    assign_public_link_to_exam = models.BooleanField(null=True)
    enable_institute_api_key = models.BooleanField(null=True)
    # apply_minimum_wage = models.BooleanField(null=True)
    # apply_wage_factor = models.BooleanField(null=True)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    modified_at = models.DateTimeField(auto_now=True, db_index=True)

    def _str_(self):
        return self.name


class UserPlan(models.Model):
    id = models.BigAutoField(primary_key=True, unique=True, db_index=True)
    name = models.CharField(max_length=46)
    user = models.ForeignKey(User, on_delete=models.CASCADE, db_index=True)
    start_date = models.DateTimeField(db_index=True)
    end_date = models.DateTimeField(db_index=True, null=True)
    purchased_price = models.IntegerField(null=True)
    wage_factor = models.FloatField(null=True)
    tax_factor = models.FloatField(null=True)
    min_wage_fee = models.IntegerField(null=True)
    max_institutes_per_user = models.IntegerField(null=True)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    modified_at = models.DateTimeField(auto_now=True, db_index=True)

    def _str_(self):
        return self.name


class InstitutePlan(models.Model):
    id = models.BigAutoField(primary_key=True, unique=True, db_index=True)
    name = models.CharField(max_length=46)
    institute = models.ForeignKey(Institute, on_delete=models.CASCADE, db_index=True)
    start_date = models.DateTimeField(db_index=True)
    end_date = models.DateTimeField(db_index=True, null=True)
    purchased_price = models.IntegerField(null=True)
    max_collaborators_per_institute = models.IntegerField(null=True)
    max_question_banks_per_institute = models.IntegerField(null=True)
    max_questions_per_question_bank = models.IntegerField(null=True)
    max_examinee_per_institute = models.IntegerField(null=True)
    max_active_exam_per_institute = models.IntegerField(null=True)
    assign_examinee_to_exam = models.BooleanField(null=True)
    assign_public_link_to_exam = models.BooleanField(null=True)
    enable_institute_api_key = models.BooleanField(null=True)
    # apply_minimum_wage = models.BooleanField(null=True)
    # apply_wage_factor = models.BooleanField(null=True)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    modified_at = models.DateTimeField(auto_now=True, db_index=True)

    def _str_(self):
        return self.name
