from django.db import models
from django.urls import reverse

from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType

from django.utils.translation import gettext_lazy as _


class Category(models.Model):

    name = models.CharField(unique=True, max_length=64)

    class Meta:
        verbose_name = _("Category")
        verbose_name_plural = _("Categories")

    def __str__(self):
        return self.name.capitalize()


class Tags(models.Model):

    tag_slug        = models.CharField(unique=True, max_length=64,)
    date_added      = models.DateTimeField(auto_now_add=True, editable=True)
    category        = models.ManyToManyField(Category)
    
    content_type    = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True)
    object_id       = models.PositiveBigIntegerField(null=True)
    content_object  = GenericForeignKey('content_type', 'object_id')

    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

    def __str__(self):
        return self.tag_slug

        
class Users(models.Model):

    user_hash       = models.CharField(unique=True, max_length=64,)
    date_added      = models.DateTimeField(auto_now_add=True, editable=True)
    category        = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
    
    content_type    = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True)
    object_id       = models.PositiveBigIntegerField(null=True)
    content_object  = GenericForeignKey('content_type', 'object_id')
    

    class Meta:
        verbose_name = _("user")
        verbose_name_plural = _("Users")

    def __str__(self):
        return self.content_object.artist

    def get_absolute_url(self):
        return reverse('sites:artist_profile', args=[self.user_hash])
    

class Submissions(models.Model):

    submission_hash    = models.CharField(unique=True, max_length=64,)
    date               = models.DateTimeField(null=True, editable=True)
    date_added         = models.DateTimeField(auto_now_add=True, editable=True)
    category           = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
    author             = models.ForeignKey(Users, on_delete=models.CASCADE, null=True)
    
    mature             = models.BooleanField(default=False)
    
    content_type       = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True)
    object_id          = models.PositiveBigIntegerField(null=True)
    content_object     = GenericForeignKey('content_type', 'object_id')
    
   
    tags               = models.ManyToManyField(Tags)

    custom_tags        = models.ManyToManyField(CustomTags)
    
    description_length = models.PositiveIntegerField(default=0)

    class Meta:
        verbose_name = _("Submission")
        verbose_name_plural = _("Submissions")
        
        indexes = [
            models.Index(fields=['content_type', 'object_id']),
        ]

    def __str__(self):
        return self.submission_hash

    def get_absolute_url(self):
        return reverse("sites:submission", args=[self.submission_hash])