import json

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

class TtActivityTemplate(models.Model):
    code = models.CharField(max_length=150)
    name = models.CharField(max_length=255)
    desc = models.TextField(null=True)
    activity_type = models.ForeignKey("api.TtActivityType", on_delete=models.SET_NULL, null=True)
    academic_term = models.ForeignKey("api.TtAcademicTerm", on_delete=models.CASCADE, null=True)
    department = models.ForeignKey("api.TtDepartment", on_delete=models.SET_NULL, null=True)
    zone = models.ForeignKey("api.TtZone", on_delete=models.SET_NULL, null=True)
    duration = models.IntegerField(null=True,db_comment="in minute")
    slot_required = models.IntegerField(null=True)
    sequence_number = models.IntegerField(null=True)
    module = models.ForeignKey("api.TtModule", on_delete=models.CASCADE, null=True)
    planned_size = models.IntegerField(null=True)
    staff_requirement_type = models.IntegerField(default=0, db_comment="0-None, 1-wildcard, 2-preset")
    staff_requirement = models.IntegerField(default=0, db_comment="if wildcard only will set this value, if got set activity template staff, this will update to 0")
    location_requirement_type = models.IntegerField(default=0, db_comment="0-None, 1-wildcard, 2-preset")
    location_requirement = models.IntegerField(default=0, db_comment="if wildcard only will set this value, if got set activity template location, this will update to 0")
    week_pattern = models.ForeignKey("api.TtWeekPattern",on_delete=models.SET_NULL,null=True,blank=True,db_comment="NULL means custom")
    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
    week = models.ManyToManyField("api.TtWeek",through="api.TtActivityTemplateWeek")
    staff = models.ManyToManyField("api.TtStaff",through="api.TtActivityTemplateStaff")
    staff_suitability = models.ManyToManyField("api.TtSuitability",through="api.TtActivityTemplateStaffSuitability",related_name="activity_template_staff")
    location = models.ManyToManyField("api.TtLocation",through="api.TtActivityTemplateLocation")
    location_suitability = models.ManyToManyField("api.TtSuitability",through="api.TtActivityTemplateLocationSuitability",related_name="activity_template_location")

    # 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,
    }

    STAFF_REQUIREMENT_TYPE = {
        "none": 0,
        "wildcard": 1,
        "preset": 2,
    }

    LOCATION_REQUIREMENT_TYPE = {
        "none": 0,
        "wildcard": 1,
        "preset": 2,
    }

    # table need display for delete confirmation
    DELETE_CONFIRMATION_TABLE = [
        "tt_activity",
    ]

    IMPORT_RULES = {
        "code": ["required", "string", "unique"],
        "name": ["required", "string"],
        "desc": ["nullable", "string"],
        "activity_type_id": ["required", "exist"], # activity_type code
        "department_id": ["nullable", "exist"], # department code
        "sequence_number": ["nullable", "integer"],
        "zone_id": ["nullable", "exist"], # zone code
        "slot_required": ["required", "integer"],
        "module_id": ["required", "exist"], # module code, cant update
        "planned_size": ["required", "integer"],
        "staff_requirement": ["nullable", "integer"],
        "location_requirement": ["nullable", "integer"],
        "week_pattern_id": ["nullable", "exist"], # week_pattern code
        "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_activity_template"

    def __str__(self):
        return self.name