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

from django.utils.translation import gettext_lazy as _

# Create your models here.

def get_upload_to(instance, filename, folder):
    return f'{folder}/{instance.file_hash[:2]}/{filename}'

def get_upload_to_metadata(instance, filename):
    return get_upload_to(instance, filename, 'metadata')

def get_upload_to_submission(instance, filename):
    return get_upload_to(instance, filename, 'submissions')

def get_upload_to_profile(instance, filename):
    return get_upload_to(instance, filename, 'profiles')

def get_upload_to_banner(instance, filename):
    return get_upload_to(instance, filename, 'banners')


class User_Profile_Images(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=get_upload_to_profile, blank=True)
    file_mime  = models.CharField(max_length=64, blank=True)
    file_ext   = models.CharField(max_length=64, blank=True)
    size       = models.PositiveIntegerField(null=True)
    date_added = models.DateTimeField(auto_now_add=True, editable=True)

    class Meta:
        verbose_name = _("User Profile Image")
        verbose_name_plural = _("User Profile Images")

    def __str__(self):
        return self.file_name

    def get_absolute_url(self):
        return reverse('files:serve_content_file', args=['user_profile', self.file_hash])


class User_Banner_Images(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=get_upload_to_banner, blank=True)
    file_mime  = models.CharField(max_length=64, blank=True)
    file_ext   = models.CharField(max_length=64, blank=True)
    size       = models.PositiveIntegerField(null=True)
    date_added = models.DateTimeField(auto_now_add=True, editable=True)

    class Meta:
        verbose_name = _("User Banner Image")
        verbose_name_plural = _("User Banner Images")

    def __str__(self):
        return self.file_name
    
    def get_absolute_url(self):
        return reverse('files:serve_content_file', args=['user_banner', self.file_hash])


class 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=get_upload_to_submission, blank=True)
    file_mime    = models.CharField(max_length=64, blank=True)
    file_ext     = models.CharField(max_length=64, blank=True)
    size         = models.PositiveIntegerField(null=True)
    date_added   = models.DateTimeField(auto_now_add=True, editable=True)

    extra_file   = models.BooleanField(default=False)
    file_source  = models.CharField(max_length=32, blank=True)

    image_height = models.PositiveIntegerField(null=True)
    image_width  = models.PositiveIntegerField(null=True)

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

    def __str__(self):
        return self.file_name

    def get_absolute_url(self):
        return reverse('files:serve_content_file', args=['submission', self.file_hash])


class Metadata_Files(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=get_upload_to_metadata, blank=True)
    file_mime  = models.CharField(max_length=64, blank=True)
    file_ext   = models.CharField(max_length=64, blank=True)
    size       = models.PositiveIntegerField(null=True)
    date_added = models.DateTimeField(auto_now_add=True, editable=True)

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

    def __str__(self):
        return self.file_name
    
    def get_absolute_url(self):
        return reverse('files:serve_content_file', args=['metadata', self.file_hash])
