import json

from django.contrib.contenttypes.fields import GenericRelation
from django.db import models

class TtStaff(models.Model):

    code = models.CharField(max_length=150)
    name = models.CharField(max_length=255)
    email = models.EmailField(null=True)
    desc = models.TextField(null=True)
    department = models.ForeignKey("api.TtDepartment", on_delete=models.SET_NULL,null=True)
    is_part_time = models.BooleanField(default=False)
    maximum_period = models.IntegerField(null=True)
    contract_period = models.IntegerField(null=True)
    shared_with_all_department = models.BooleanField(default=False)
    zone = models.ForeignKey("api.TtZone", on_delete=models.SET_NULL, null=True)
    availability = models.ForeignKey("api.TtAvailability",on_delete=models.SET_NULL,null=True,blank=True,db_comment="NULL means custom")
    availability_pattern = models.TextField(null=True)
    start_preference = models.ForeignKey("api.TtStartPreference",on_delete=models.SET_NULL,null=True,blank=True,db_comment="NULL means custom")
    start_preference_pattern = models.TextField(null=True)
    usage_preference = models.ForeignKey("api.TtUsagePreference",on_delete=models.SET_NULL,null=True,blank=True,db_comment="NULL means custom")
    usage_preference_pattern = models.TextField(null=True)
    extra_data_1 = models.TextField(null=True)
    extra_data_2 = models.TextField(null=True)
    extra_data_3 = models.TextField(null=True)
    extra_data_4 = models.TextField(null=True)
    extra_data_5 = models.TextField(null=True)
    extra_data_6 = models.TextField(null=True)
    extra_data_7 = models.TextField(null=True)
    extra_data_8 = models.TextField(null=True)
    extra_data_9 = models.TextField(null=True)
    extra_data_10 = models.TextField(null=True)
    status = models.SmallIntegerField()
    created_by = models.BigIntegerField(null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_by = models.BigIntegerField(null=True)
    updated_at = models.DateTimeField(auto_now=True)

    # having this line wont add column in db, it make the function like TtStaff.avoid_concurrency.add() can be used
    avoid_concurrency = models.ManyToManyField("self",through="api.TtStaffAvoidConcurrency",symmetrical=False)
    constraint_profile = models.ManyToManyField("api.TtConstraintProfile",through="api.TtStaffConstraintProfile")
    shared_with_department = models.ManyToManyField("api.TtDepartment",through="api.TtStaffSharedWithDepartment",related_name="staff_shared_with_department")
    suitability = models.ManyToManyField("api.TtSuitability",through="api.TtStaffSuitability")
    resource_map = models.ManyToManyField("api.TtWeek",through="api.TtStaffResourceMap")

    # wont add column, just want create relation between staff and tt_tag_relation table
    tag = GenericRelation(
        "api.TtTagRelation",
        content_type_field="refer_table",
        object_id_field="refer_id",
    )

    STATUS_TO_CODE = {
        "active": 1,
        "inactive": 2,
    }

    WORK_TIME = {
        "hours_per_day": 8,
        "days_per_week": 5
    }

    IMPORT_RULES = {
        "code": ["required", "string", "unique"],
        "email": ["nullable", "string", "unique"],
        "name": ["required", "string"],
        "desc": ["nullable", "string"],
        "department_id": ["nullable", "exist"], # department code
        "zone_id": ["nullable", "exist"], # zone code
        "is_part_time": ["nullable", "boolean"], # 0 or 1, true or false
        "maximum_period": ["nullable", "integer"], # default calculation
        "contract_period": ["nullable", "integer"], # default calculation 
        "shared_with_all_department": ["nullable", "boolean"], # 0 or 1, true or false
        "availability_id": ["nullable", "exist"], # availability code
        "availability_pattern": ["nullable", "string"],
        "start_preference_id": ["nullable", "exist"], # start_preference code
        "start_preference_pattern": ["nullable", "string"],
        "usage_preference_id": ["nullable", "exist"], # usage_preference code
        "usage_preference_pattern": ["nullable", "string"],
        **{
            f"extra_data_{i}": ["nullable", "string"]
            for i in range(1, 11)
        },
    }

    class Meta:
        db_table = "tt_staff"

    def __str__(self):
        return self.name
    
