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

# ---------------------------------------------------------------------------
# Control
# ---------------------------------------------------------------------------
class Control(models.Model):
    """Security control/measure linked to a risk."""

    class Meta:
        verbose_name = _("Control")
        verbose_name_plural = _("Controls")

    STATUS_CHOICES = [
        ("planned", _("Planned")),
        ("in_progress", _("In progress")),
        ("completed", _("Completed")),
        ("verified", _("Verified")),
        ("rejected", _("Rejected")),
    ]

    title = models.CharField(_("Title"), max_length=255)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="planned")
    due_date = models.DateField(blank=True, null=True)
    responsible = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        related_name="responsible_controls"
    )
    description = models.TextField(blank=True, null=True)
    wiki_link = models.URLField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    # Relation to risk
    risks = models.ManyToManyField("Risk", related_name="controls", blank=True)

    def __str__(self):
        return f"{self.title} ({self.get_status_display()})"