from django.utils.translation import gettext_lazy as _
from django.utils.text import slugify
from django.urls import reverse

from django.db import models

import os

# Create your models here.

class FA_UserIconFile(models.Model):

    icon_file_hash  = models.CharField(unique=True, max_length=64,)
    icon_file_name  = models.CharField(max_length=150, blank=True)
    icon_file       = models.FileField(upload_to="furaffinity/user_icon/", blank=True)
    date_added      = models.DateTimeField(auto_now_add=True, editable=True)    

    class Meta:
        verbose_name = _("User Icon File")
        verbose_name_plural = _("User Icon Files")

    def __str__(self):
        return self.name

    #def get_absolute_url(self):
    #    return reverse("FA_UserIconFile_detail", kwargs={"pk": self.pk})


class FA_User(models.Model):

    artist      = models.CharField(max_length=35, unique=False,)
    artist_url  = models.SlugField(max_length=40, unique=True,)
    description = models.TextField(blank=True)
    icon        = models.ForeignKey(FA_UserIconFile, on_delete=models.CASCADE, blank=True, null=True)
    

    def __unicode__(self):
        return self.word

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

    def __str__(self):
        return self.artist

    def get_absolute_url(self):
        return f"/fa/user/{self.artist_url}"
        #return reverse("fa_user", kwargs={"pk": self.pk})


class FA_Tags(models.Model):
    
    tag      = models.CharField(verbose_name="Tag name", max_length=250, unique=True)
    tag_slug = models.SlugField(verbose_name="Tag slug/url", max_length=260, unique=True)

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

    def save(self, *args, **kwargs):
    # add slug if not already exists on save.
        if not self.tag_slug:
            self.tag_slug = slugify(self.tag)
        super().save(*args, **kwargs)
    
    def __str__(self):
        return self.tag
    
    def get_absolute_url(self):
        return f"/fa/tag/{self.tag_slug}"

#    def get_absolute_url(self):
#        return f"/fa/tag/{self.tag_url}"
#        return reverse("FA_Tag_detail", kwargs={"pk": self.pk})

class FA_Species(models.Model):

    species = models.CharField(unique=True, max_length=50)

    class Meta:
        verbose_name = _("Species")
        verbose_name_plural = _("Species")

    def __str__(self):
        return self.species

#    def get_absolute_url(self):
#        return reverse("FA_Species_detail", kwargs={"pk": self.pk})

class FA_Gender(models.Model):

    gender = models.CharField(unique=True, max_length=30)

    class Meta:
        verbose_name = _("Gender")
        verbose_name_plural = _("Genders")

    def __str__(self):
        return self.gender

#    def get_absolute_url(self):
#        return reverse("FA_Gender_detail", kwargs={"pk": self.pk})

class FA_Mature(models.Model):

    mature = models.CharField(max_length=30, unique=True) #verbose_name="Mature Rating",

    class Meta:
        verbose_name = _("Mature Rating")
        verbose_name_plural = _("Mature Ratings")

    def __str__(self):
        return self.mature

    #def get_absolute_url(self):
    #    return reverse("FA_Mature_detail", kwargs={"pk": self.pk})

class FA_Submission_File(models.Model):

    file_hash  = models.CharField(unique=True, max_length=64,)
    file_name  = models.CharField(max_length=150, blank=True)
    file       = models.FileField(upload_to="furaffinity/submissions/", blank=True)
    date_added = models.DateTimeField(auto_now_add=True, editable=True)

    class Meta:
        verbose_name = _("Submission File")
        verbose_name_plural = _("Submission Files")

    def __str__(self):
        return self.file_hash

    #def delete(self, *args, **kwargs):
    #    # Delete the file from the disk when the model instance is deleted
    #    print('Deleting image:', self.file)
    #    if self.file:
    #        if os.path.isfile(self.file.path):
    #            os.remove(self.file.path)
    #
    #    super().delete(*args, **kwargs)

    #def get_absolute_url(self):
    #    return reverse("Submission_Image_detail", kwargs={"pk": self.pk})



class FA_Submission(models.Model):

    MATURE = [
        ("G", "General"),
        ("M", "Mature"),
        ("A", "Adult"),
        ]
    
    GENDER = [
        ("Ma", "Male"),
        ("Fe", "Female"),
        ("He", "Herm"),
        ("In", "Intersex"),
        ("TM", "Trans (Male)"),
        ("TF", "Trans (Female)"),
        ("NB", "Non-Binary"),
        ("MC", "Multiple characters"), 
        ("ON", "Other / Not Specified"),
        ] 

    # id
    submission_id      = models.PositiveBigIntegerField(verbose_name="ID", primary_key=True, unique=True, default=1)
    # url
    media_url          = models.URLField(blank=True)
    # title
    title              = models.CharField(default="", max_length=60)
    # description
    description        = models.TextField(default="", null=True, blank=True)
    # artist
    artist             = models.ForeignKey(FA_User, on_delete=models.CASCADE, null=True, related_name='submissions')
    # date
    date               = models.DateTimeField(null=True)
    # NOT imported
    date_added         = models.DateTimeField(auto_now_add=True, editable=True)
    # species
    species            = models.ForeignKey(FA_Species, on_delete=models.CASCADE, null=True)
    # tags
    tags               = models.ManyToManyField(FA_Tags)
    # rating
    mature_rating      = models.ForeignKey(FA_Mature, on_delete=models.CASCADE, null=True) #CharField(max_length=2, choices=MATURE, default=MATURE[0][0])
    # comments
    number_of_comments = models.PositiveIntegerField(null=True)
    # views
    views              = models.PositiveIntegerField(null=True)
    # gender
    gender             = models.ForeignKey(FA_Gender, on_delete=models.CASCADE, null=True)
    # theme
    fa_theme           = models.CharField(verbose_name="FA Theme", max_length=50, null=True)
    # fa_category
    fa_category        = models.CharField(verbose_name="FA Category", max_length=50, null=True)
    # subcategory
    gallery_type       = models.CharField(max_length=20, blank=True)
    # extension
    file_extension     = models.CharField(max_length=10, blank=True)
    # height
    image_height       = models.PositiveIntegerField(blank=True, null=True)
    # width
    image_width        = models.PositiveIntegerField(blank=True, null=True)
    # ImageField for the image files
    file               = models.ForeignKey(FA_Submission_File, on_delete=models.CASCADE, blank=True, null=True)  
    
    
    class Meta:
        verbose_name = _("Submission")
        verbose_name_plural = _("Submissions")

    def __str__(self):
        return self.title
    
    #def save(self, *args, **kwargs):
    #    if not self.related_model:
    #        self.related_model, created = FA_User.objects.get_or_create(name=self.artist)
    #    super().save(*args, **kwargs)

#    def get_absolute_url(self):
#        return reverse("FA_Submission_detail", kwargs={"pk": self.pk})