from django.db import models


class ClinicalRotationPlanItem(models.Model):
    plan = models.ForeignKey(
        "api.ClinicalRotationPlan", on_delete=models.CASCADE, related_name="items"
    )
    round_index = models.PositiveIntegerField()
    activity = models.ForeignKey("api.TtActivity", on_delete=models.PROTECT)
    student_set = models.ForeignKey("api.TtStudentSet", on_delete=models.PROTECT)
    activity_template = models.ForeignKey("api.TtActivityTemplate", on_delete=models.PROTECT)
    start_slot = models.IntegerField(null=True, blank=True)
    end_slot_exclusive = models.IntegerField(null=True, blank=True)
    staff_ids = models.JSONField(default=list)
    location_ids = models.JSONField(default=list)
    allocation_snapshot = models.JSONField(default=dict)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = "clinical_rotation_plan_item"
        constraints = [
            models.UniqueConstraint(
                fields=["plan", "activity"], name="uq_cr_item_plan_activity"
            ),
            models.UniqueConstraint(
                fields=["plan", "round_index", "student_set"],
                name="uq_cr_item_round_set",
            ),
            models.UniqueConstraint(
                fields=["plan", "round_index", "activity_template"],
                name="uq_cr_item_round_template",
            ),
        ]
        ordering = ["round_index", "student_set_id"]
