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


class EsSuitability(models.Model):
    code = models.CharField(max_length=100)
    name = models.CharField(max_length=100)
    desc = models.TextField(null=True)
    department = models.ForeignKey("api.EsDepartment", 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)

    invigilator = models.ManyToManyField("api.EsInvigilator", through="api.EsInvigilatorSuitability")
    location = models.ManyToManyField("api.EsLocation", through="api.EsLocationSuitability")

    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"],
        "desc": ["nullable", "string"],
        "department_id": ["nullable", "exist"],
        "tag": ["nullable", "string"],
        **{f"extra_data_{i}": ["nullable", "string"] for i in range(1, 11)},
    }

    SUITABILITY_INVIGILATOR = {
        "IMPORT_RULES": {
            "suitability_id": ["required", "string"],
            "invigilator_id": ["required", "string"],
        }
    }

    SUITABILITY_LOCATION = {
        "IMPORT_RULES": {
            "suitability_id": ["required", "string"],
            "location_id": ["required", "string"],
        }
    }

    class Meta:
        db_table = "es_suitability"

    def __str__(self):
        return self.name