from django.db import models


class FarmAlertNotification(models.Model):
    LEVEL_INFO = "info"
    LEVEL_WARNING = "warning"
    LEVEL_DANGER = "danger"
    LEVEL_CHOICES = [
        (LEVEL_INFO, "اطلاع رسانی"),
        (LEVEL_WARNING, "هشدار"),
        (LEVEL_DANGER, "خطر"),
    ]

    ENDPOINT_TRACKER = "tracker"
    ENDPOINT_TIMELINE = "timeline"
    ENDPOINT_CHOICES = [
        (ENDPOINT_TRACKER, "Tracker"),
        (ENDPOINT_TIMELINE, "Timeline"),
    ]

    farm_uuid = models.UUIDField(db_index=True)
    endpoint = models.CharField(max_length=32, choices=ENDPOINT_CHOICES, db_index=True)
    level = models.CharField(max_length=16, choices=LEVEL_CHOICES, db_index=True)
    title = models.CharField(max_length=255)
    message = models.TextField(blank=True)
    suggested_action = models.TextField(blank=True)
    source_alert_id = models.CharField(max_length=255, blank=True, db_index=True)
    source_metric_type = models.CharField(max_length=64, blank=True, db_index=True)
    fingerprint = models.CharField(max_length=64, unique=True)
    payload = models.JSONField(default=dict, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = "farm_alerts_notification"
        ordering = ["-updated_at", "-created_at"]
        indexes = [
            models.Index(fields=["farm_uuid", "endpoint", "-updated_at"]),
            models.Index(fields=["farm_uuid", "level", "-updated_at"]),
        ]
        verbose_name = "Farm Alert Notification"
        verbose_name_plural = "Farm Alert Notifications"

    def __str__(self):
        return f"{self.farm_uuid} - {self.endpoint} - {self.level} - {self.title}"
