from django.db import models
from django.conf import settings
from django.utils.translation import gettext_lazy as _

# ---------------------------------------------------------------------------
# NotificationPreference
# ---------------------------------------------------------------------------
class NotificationPreference(models.Model):
    """User-specific notification preferences."""

    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name="notification_preference",
        verbose_name=_("User"),
    )

    # Risks
    risk_created = models.BooleanField(default=True)
    risk_updated = models.BooleanField(default=True)
    risk_deleted = models.BooleanField(default=True)

    # Controls
    control_created = models.BooleanField(default=True)
    control_updated = models.BooleanField(default=True)
    control_deleted = models.BooleanField(default=True)

    # Residual risks
    residual_created = models.BooleanField(default=True)
    residual_updated = models.BooleanField(default=True)
    residual_deleted = models.BooleanField(default=True)

    # Reviews
    review_required = models.BooleanField(default=True)
    review_completed = models.BooleanField(default=True)

    # Users
    user_created = models.BooleanField(default=False)
    user_deleted = models.BooleanField(default=False)

    # Incidents
    incident_created = models.BooleanField(default=True)
    incident_updated = models.BooleanField(default=True)
    incident_deleted = models.BooleanField(default=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"Prefs({self.user})"

    def should_notify(self, event_code: str) -> bool:
        """Return True if user wants notifications for this event code."""
        return bool(getattr(self, event_code, False))
