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


class EsDepartment(models.Model):
    tt_id = models.BigIntegerField(null=True, unique=True, db_comment="Timetabler department id for Kafka sync")
    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,related_name="sub_department") # related name is used for get sub department
    zone = models.ForeignKey("api.EsZone", 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)

    # wont add column, just want create relation between self and es_tag_relation table
    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"], # department code
        "zone_id": ["nullable", "exist"], # zone code
        "tag": ["nullable", "string"],
        **{f"extra_data_{i}": ["nullable", "string"] for i in range(1, 11)},
    }

    class Meta:
        db_table = "es_department"

    def __str__(self):
        return self.name