from django.db import models
from django.conf import settings
from django.db.models.signals import pre_delete
from django.dispatch import receiver


class APILog(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True)
    request_url = models.URLField()
    request_params = models.CharField(max_length=255, null=True, blank=True)
    request_method = models.CharField(max_length=10)
    request_body = models.TextField()
    request_timestamp = models.DateTimeField(auto_now_add=True)

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


@receiver(pre_delete, sender=APILog)
def prevent_delete(sender, instance, **kwargs):
    # Empêchez la suppression en levant une exception, par exemple ValueError
    raise ValueError("Vous n'êtes pas autorisé à supprimer cet enregistrement.")