from django.db import models


class UserHasNotification(models.Model):
    user = models.ForeignKey(
        "user_management.User",
        on_delete=models.CASCADE,
    )

    alert_group = models.ForeignKey(
        "alerts.AlertGroup",
        on_delete=models.CASCADE,
    )

    active_notification_policy_id = models.CharField(max_length=100, null=True, default=None)  # ID generated by celery

    class Meta:
        unique_together = ("user", "alert_group")

    def update_active_task_id(self, task_id):
        """
        `active_notification_policy_id` keeps celery task_id of the next scheduled `notify_user_task`
        for the current user
        """
        self.active_notification_policy_id = task_id
        self.save(update_fields=["active_notification_policy_id"])
