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


class VerificationMode(models.TextChoices):
    """How a verification concern (implants, fuel, boosters, FEB) is enforced.

    A per-item ``SubstitutionPolicy.ANY`` always means "no requirement" and is
    skipped before the mode is consulted - the mode governs how *real*
    requirements are treated."""

    REJECT = "REJECT", _("Reject - Always Enforce")
    POLICY = "POLICY", _("Enforce by policy")
    WARN = "WARN", _("Pass - Warning")
    IGNORE = "IGNORE", _("Pass - Never Enforce")


class EnforcementSettings(models.Model):
    """Singleton of site-wide enforcement modes for the verification concerns the
    compliance engine can't always verify (implants/fuel/boosters from EFT, FEB
    from ESI). Defaults preserve the historical behaviour: implants enforced by
    policy; fuel and boosters warn-only; FEB not checked."""

    implant_mode = models.CharField(
        max_length=6, choices=VerificationMode.choices, default=VerificationMode.POLICY
    )
    fuel_mode = models.CharField(
        max_length=6, choices=VerificationMode.choices, default=VerificationMode.WARN
    )
    booster_mode = models.CharField(
        max_length=6, choices=VerificationMode.choices, default=VerificationMode.WARN
    )
    feb_mode = models.CharField(
        max_length=6, choices=VerificationMode.choices, default=VerificationMode.IGNORE
    )
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "Enforcement settings"
        verbose_name_plural = "Enforcement settings"

    def __str__(self) -> str:
        return "Fit Check enforcement settings"

    def save(self, *args, **kwargs):
        self.pk = 1  # enforce a single row
        super().save(*args, **kwargs)

    @classmethod
    def current(cls) -> "EnforcementSettings":
        """The single settings row, created with defaults on first access."""
        obj, _created = cls.objects.get_or_create(pk=1)
        return obj
