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

class EsInvigilator(models.Model):
    code = models.CharField(max_length=150)
    name = models.CharField(max_length=150)
    email = models.EmailField(null=True)
    phone = models.CharField(max_length=150,null=True)
    desc = models.TextField(null=True)
    department = models.ForeignKey("api.EsDepartment", on_delete=models.SET_NULL,null=True)
    staff = models.ForeignKey("api.EsStaff", on_delete=models.SET_NULL, null=True, unique=True)
    engagement_date = models.DateField(null=True)
    termination_date = models.DateField(null=True)
    status = models.SmallIntegerField()
    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)
    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)

    suitability = models.ManyToManyField("api.EsSuitability", through="api.EsInvigilatorSuitability")
    role = models.ManyToManyField("api.EsInvigilatorRole",through="api.EsInvigilatorInvigilatorRole")

    tag = GenericRelation(
        "api.EsTagRelation",
        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"],
        "phone": ["nullable", "string"],
        "desc": ["nullable", "string"],
        "department_id": ["nullable", "exist"],
        "staff_id": ["nullable", "exist"],
        "engagement_date": ["nullable", "date"],
        "termination_date": ["nullable", "date"],
        "tag": ["nullable", "string"],
        **{f"extra_data_{i}": ["nullable", "string"] for i in range(1, 11)},
    }

    INVIGILATOR_ROLES = {
        "IMPORT_RULES": {
            "invigilator_id": ["required", "string"],
            "role_id": ["required", "string"],
        }
    }

    class Meta:
        db_table = "es_invigilator"

    def __str__(self):
        return self.name
    
