from django.db import models
from . import BaseModel, CustomUser
import uuid

class FailedApiCall(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True)
    date = models.DateTimeField()
    user = models.ForeignKey(CustomUser, blank=True, null=True, on_delete=models.SET_NULL, related_name='failed_api_calls')
    type = models.CharField(max_length=50)
    call_id = models.UUIDField()
    error = models.TextField()
    api_call = models.JSONField()
    authentication = models.TextField(blank=True, null=True)

    def __str__(self):
        # datetime = self.date.strftime("%Y-%m-%d %H:%M")
        if self.user:
            return f"{self.user.username} | {self.type} : {self.error}"
        else:
            return f"{self.type} : {self.error}"

    def discord_string(self):
        return str(self)
