import json

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

class TtLocation(models.Model):
    code = models.CharField(max_length=150)
    name = models.CharField(max_length=255)
    desc = models.TextField(null=True)
    department = models.ForeignKey("api.TtDepartment", on_delete=models.SET_NULL, null=True)
    capacity = models.IntegerField(null=True)
    area = models.IntegerField(null=True)
    maximum_period = models.IntegerField(null=True)
    contract_period = models.IntegerField(null=True)
    is_online = models.BooleanField(default=False)
    shared_with_all_department = models.BooleanField(default=False)
    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 TtLocation.avoid_concurrency.add() can be used
    shared_with_department = models.ManyToManyField("api.TtDepartment",through="api.TtLocationSharedWithDepartment",related_name="location_shared_with_department")

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

    class Meta:
        db_table = "tt_location"

    def __str__(self):
        return self.name