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

class EsLocation(models.Model):
    tt_id = models.BigIntegerField(null=True, unique=True, db_comment="Timetabler location id for Kafka sync")
    code = models.CharField(max_length=150)
    name = models.CharField(max_length=150)
    desc = models.TextField(null=True)
    seat_order = models.SmallIntegerField(db_comment="1 - Column First, 2 - Row First")
    row = models.IntegerField(default=0)
    column = models.IntegerField(default=0)
    is_partition = models.BooleanField(default=False)
    zone = models.ForeignKey("api.EsZone", on_delete=models.SET_NULL, null=True)
    department = models.ForeignKey("api.EsDepartment", on_delete=models.SET_NULL, null=True)
    status = models.SmallIntegerField(db_comment="1 - Active, 2 - Inactive")
    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.EsLocationSuitability")

    # having this line will not add column in db, it makes the function like TtLocation.avoid_concurrency.add() can be used
    #avoid_concurrency = models.ManyToManyField("self", through="api.TtLocationAvoidConcurrency", symmetrical=False)
    #constraint_profile = models.ManyToManyField("api.TtConstraintProfile",through="api.TtLocationConstraintProfile")
    #shared_with_department = models.ManyToManyField("api.TtDepartment",through="api.TtLocationSharedWithDepartment",related_name="location_shared_with_department")
    #suitability = models.ManyToManyField("api.TtSuitability",through="api.TtLocationSuitability")
    #resource_map = models.ManyToManyField("api.TtWeek",through="api.TtLocationResourceMap")

    # will not add column, just want to create relation between staff and es_tag_relation table
    tag = GenericRelation(
        "api.EsTagRelation",
        content_type_field="refer_table",
        object_id_field="refer_id",
    )

    SEAT_ORDER_TO_CODE = {
        "column_first": 1,
        "row_first": 2,
    }

    STATUS_TO_CODE = {
        "active": 1,
        "inactive": 2,
    }

    IMPORT_RULES = {
        "code": ["required", "string", "unique"],
        "name": ["required", "string"],
        "desc": ["nullable", "string"],
        "seat_order": ["required", "integer"],
        "row": ["nullable", "integer"],
        "column": ["nullable", "integer"],
        "is_partition": ["nullable", "boolean"],
        "zone_id": ["nullable", "exist"],
        "department_id": ["nullable", "exist"],
        "tag": ["nullable", "string"],
        **{f"extra_data_{i}": ["nullable", "string"] for i in range(1, 11)},
    }

    class Meta:
        db_table = "es_location"

    def __str__(self):
        return self.name