import json

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

class TtLocation(models.Model):
    code = models.CharField(max_length=150)
    name = models.CharField(max_length=255)
    desc = models.TextField(null=True)
    department = models.ForeignKey("api.TtDepartment", on_delete=models.SET_NULL, null=True)
    capacity = models.IntegerField(null=True, default=0)
    area = models.IntegerField(null=True)
    maximum_period = models.IntegerField(null=True)
    contract_period = models.IntegerField(null=True)
    is_online = models.BooleanField(default=False)
    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 TtLocation.avoid_concurrency.add() can be used
    avoid_concurrency = models.ManyToManyField("self", through="api.TtLocationAvoidConcurrency", symmetrical=False)
    constraint_profile = models.ManyToManyField("api.TtConstraintProfile",through="api.TtLocationConstraintProfile")
    shared_with_department = models.ManyToManyField("api.TtDepartment",through="api.TtLocationSharedWithDepartment",related_name="location_shared_with_department")
    suitability = models.ManyToManyField("api.TtSuitability",through="api.TtLocationSuitability")
    resource_map = models.ManyToManyField("api.TtWeek",through="api.TtLocationResourceMap")

    # 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"],
        "name": ["required", "string"],
        "desc": ["nullable", "string"],
        "department_id": ["nullable", "exist"], # department code
        "zone_id": ["nullable", "exist"], # zone code
        "capacity": ["nullable", "integer"],
        "area": ["nullable", "integer"],
        "maximum_period": ["nullable", "integer"], # default calculation
        "contract_period": ["nullable", "integer"], # default calculation
        "is_online": ["nullable", "boolean"], # 0 or 1, true or false
        "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_location"

    def __str__(self):
        return self.name