import json

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

class TtStudent(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)
    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
    academic_term = models.ManyToManyField("api.TtAcademicTerm",through="api.TtStudentAcademicTerm")
    pos = models.ManyToManyField("api.TtPos",through="api.TtStudentPos")
    pathway = models.ManyToManyField("api.TtPathway",through="api.TtStudentPathway")
    pos_module = models.ManyToManyField("api.TtPosModuleGroupModule",through="api.TtStudentPosModule")
    resource_map = models.ManyToManyField("api.TtWeek", through="api.TtStudentResourceMap")

    # wont add column, just want create relation between student 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,
    }

    IMPORT_RULES = {
        "code": ["required", "string", "unique"],
        "name": ["required", "string"],
        "email": ["nullable", "string"],
        "desc": ["nullable", "string"],
        "department_id": ["nullable", "exist"],  # department code
        **{
            f"extra_data_{i}": ["nullable", "string"]
            for i in range(1, 11)
        },
    }

    STUDENT_ACADEMIC_TERM = {
        "IMPORT_RULES": {
            "student_id": ["required", "string"],  # student code
            "academic_term_id": ["required", "string"],  # academic term code
        }
    }

    STUDENT_POS = {
        "IMPORT_RULES": {
            "student_id": ["required", "string"],  # student code
            "pos_id": ["required", "string"],  # POS code
        }
    }

    STUDENT_POS_MODULE = {
        "IMPORT_RULES": {
            "student_id": ["required", "string"],  # student code
            "pos_module_group_id": ["required", "string"],  # POS module-group code
            "module_id": ["required", "string"],  # module code
        }
    }

    STUDENT_SET_STUDENT = {
        "IMPORT_RULES": {
            "student_set_id": ["required", "string"],  # student set code
            "student_id": ["required", "string"],  # student code
        }
    }

    class Meta:
        db_table = "tt_student"

    def __str__(self):
        return self.name
