# all relation table if need customize, will put in this file
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models

from backend import settings

class UserRole(models.Model):
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    role = models.ForeignKey("api.role", on_delete=models.CASCADE)

    class Meta:
        db_table = "user_role"
        constraints = [
            models.UniqueConstraint(fields=["user", "role"], name="unique_user_role")
        ]

class TtStaffSharedWithDepartment(models.Model):
    staff = models.ForeignKey("api.TtStaff",on_delete=models.CASCADE)
    department = models.ForeignKey("api.TtDepartment",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_staff_shared_with_department"  # custom table name
        unique_together = ("staff", "department")  # prevent duplicates

class TtLocationSharedWithDepartment(models.Model):
    location = models.ForeignKey("api.TtLocation",on_delete=models.CASCADE)
    department = models.ForeignKey("api.TtDepartment",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_location_shared_with_department"  # custom table name
        unique_together = ("location", "department")  # prevent duplicates

class TtAcademicTermWeek(models.Model):
    academic_term = models.ForeignKey("api.TtAcademicTerm",on_delete=models.CASCADE)
    week = models.ForeignKey("api.TtWeek",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_academic_term_week"  # custom table name
        unique_together = ("academic_term", "week")  # prevent duplicates

class TtWeekPatternWeek(models.Model):
    week_pattern = models.ForeignKey("api.TtWeekPattern",on_delete=models.CASCADE)
    week = models.ForeignKey("api.TtWeek",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_week_pattern_week"  # custom table name
        unique_together = ("week_pattern", "week")  # prevent duplicates

class TtPosModuleGroup(models.Model):
    pos = models.ForeignKey("api.TtPos",on_delete=models.CASCADE)
    module_group = models.ForeignKey("api.TtModuleGroup",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    module = models.ManyToManyField("api.TtModule",through="api.TtPosModuleGroupModule")
    class Meta:
        db_table = "tt_pos_module_group"  # custom table name
        unique_together = ("pos", "module_group")  # prevent duplicates

class TtPosModuleGroupModule(models.Model):
    pos_module_group = models.ForeignKey("api.TtPosModuleGroup",on_delete=models.CASCADE)
    module = models.ForeignKey("api.TtModule",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_pos_module_group_module"  # custom table name
        unique_together = ("pos_module_group", "module")  # prevent duplicates

class TtModuleWeek(models.Model):
    module = models.ForeignKey("api.TtModule",on_delete=models.CASCADE)
    week = models.ForeignKey("api.TtWeek",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_module_week"  # custom table name
        unique_together = ("module", "week")  # prevent duplicates

class TtStudentSetModule(models.Model):
    """
    This is link to pos_module_group_module table, not module table, just because name too long only make it become tt_student_set_module
    """
    student_set = models.ForeignKey("api.TtStudentSet",on_delete=models.CASCADE)
    module = models.ForeignKey("api.TtModule",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_student_set_module"  # custom table name
        unique_together = ("student_set", "module")  # prevent duplicates

class TtActivityWeek(models.Model):
    activity = models.ForeignKey("api.TtActivity",on_delete=models.CASCADE)
    week = models.ForeignKey("api.TtWeek",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_activity_week"  # custom table name
        unique_together = ("activity", "week")  # prevent duplicates

class TtActivityTemplateWeek(models.Model):
    activity_template = models.ForeignKey("api.TtActivityTemplate",on_delete=models.CASCADE)
    week = models.ForeignKey("api.TtWeek",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_activity_template_week"  # custom table name
        unique_together = ("activity_template", "week")  # prevent duplicates

class TtStudentSetActivity(models.Model):
    student_set = models.ForeignKey("api.TtStudentSet",on_delete=models.CASCADE)
    activity = models.ForeignKey("api.TtActivity",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_student_set_activity"  # custom table name
        unique_together = ("student_set","activity")  # prevent duplicates

class TtActivityStaff(models.Model):
    staff = models.ForeignKey("api.TtStaff",on_delete=models.CASCADE)
    activity = models.ForeignKey("api.TtActivity",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_activity_staff"  # custom table name
        unique_together = ("staff", "activity")  # prevent duplicates

class TtActivityLocation(models.Model):
    location = models.ForeignKey("api.TtLocation",on_delete=models.CASCADE)
    activity = models.ForeignKey("api.TtActivity",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_activity_location"  # custom table name
        unique_together = ("location", "activity")  # prevent duplicates

class UserPermissionDepartment(models.Model):
    user_permission = models.ForeignKey("api.UserPermission", on_delete=models.CASCADE)
    department = models.ForeignKey("api.TtDepartment", on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "user_permission_department"
        unique_together = ("user_permission", "department")

class RolePermissionDepartment(models.Model):
    role_permission = models.ForeignKey("api.RolePermission", on_delete=models.CASCADE)
    department = models.ForeignKey("api.TtDepartment", on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "role_permission_department"
        unique_together = ("role_permission", "department")


class TtStudentAcademicTerm(models.Model):
    student = models.ForeignKey("api.TtStudent",on_delete=models.CASCADE)
    academic_term = models.ForeignKey("api.TtAcademicTerm",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_student_academic_term"  # custom table name
        unique_together = ("student","academic_term")  # prevent duplicates

class TtStudentPos(models.Model):
    student = models.ForeignKey("api.TtStudent",on_delete=models.CASCADE)
    pos = models.ForeignKey("api.TtPos",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_student_pos"  # custom table name
        unique_together = ("student","pos")  # prevent duplicates

class TtStudentPosModule(models.Model):
    """
    This is link to TtPosModuleGroupModule
    """
    student = models.ForeignKey("api.TtStudent",on_delete=models.CASCADE)
    pos_module_group_module = models.ForeignKey("api.TtPosModuleGroupModule",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_student_pos_module"  # custom table name
        unique_together = ("student","pos_module_group_module")  # prevent duplicates

class TtStudentSetStudent(models.Model):
    student_set = models.ForeignKey("api.TtStudentSet",on_delete=models.CASCADE)
    student = models.ForeignKey("api.TtStudent",on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "tt_student_set_student"  # custom table name
        unique_together = ("student_set","student")  # prevent duplicates
