from django.db import models
from django.conf import settings
from django.contrib.auth import get_user_model
# from django.contrib.auth.models import User
from container.models import Container
from django.urls import reverse


def get_sentinel_user():
    return get_user_model().objects.get_or_create(username='deleted')[0]


class Asset(models.Model):
    named_id = models.CharField(max_length=40, unique=True)
    description = models.CharField(max_length=250, blank=True)
    quantity = models.IntegerField(default=1)
    created_ts = models.DateTimeField('datetime created', auto_now_add=True)
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user),
                                   related_name='created_assets')
    changed_ts = models.DateTimeField('datetime updated', auto_now=True)
    changed_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user),
                                   related_name='changed_assets')

    def get_absolute_url(self):
        return reverse('asset:detail', kwargs={'pk': self.pk})

    def __str__(self):
        return self.named_id + ' (' + str(self.quantity) + '): ' + self.description


class GtinProduct(models.Model):
    gtin = models.CharField(max_length=40, unique=True)
    api_request = models.CharField(max_length=250)
    api_response = models.CharField(max_length=4096)
    name = models.CharField(max_length=200, blank=True)
    brand = models.CharField(max_length=40, blank=True, null=True)
    created_ts = models.DateTimeField('datetime created', auto_now_add=True)
    changed_ts = models.DateTimeField('datetime updated', auto_now=True)

    def __str__(self):
        return self.gtin + ': ' + self.name
