# Path: src/ctf_gameserver/web/registration/fields.py
# class ThumbnailImageField(ImageField):
#     """
#     Custom variant of ImageField which automatically resizes and re-serializes an uploaded image.
#     """
# 
#     attr_class = ThumbnailImageFieldFile


from django.db import models
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from .fields import ThumbnailImageField



def _gen_image_name(instance, _):
    """
    Returns the upload path (relative to settings.MEDIA_ROOT) for the specified Team's image.
    """

    # Must "return a Unix-style path (with forward slashes)"
    return 'team-images' + '/' + str(instance.user.id) + '.png'


class Team(models.Model):
    """
    Database representation of a team participating in the competition.
    This enhances the user model, where the team name, password, formal email address etc. are stored. It is
    particularly attuned to django.contrib.auth.models.User, but should work with other user models as well.
    """

    user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True, on_delete=models.CASCADE)

    net_number = models.PositiveSmallIntegerField(null=True, unique=True)
    informal_email = models.EmailField(_('Informal email address'))

# Path: src/ctf_gameserver/web/registration/fields.py
# class ThumbnailImageField(ImageField):
#     """
#     Custom variant of ImageField which automatically resizes and re-serializes an uploaded image.
#     """
# 
#     attr_class = ThumbnailImageFieldFile


# Path: src/ctf_gameserver/web/registration/models.py
from django.db import models
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from .fields import ThumbnailImageField




def _gen_image_name(instance, _):
    """
    Returns the upload path (relative to settings.MEDIA_ROOT) for the specified Team's image.
    """

    # Must "return a Unix-style path (with forward slashes)"
    return 'team-images' + '/' + str(instance.user.id) + '.png'


class Team(models.Model):
    """
    Database representation of a team participating in the competition.
    This enhances the user model, where the team name, password, formal email address etc. are stored. It is
    particularly attuned to django.contrib.auth.models.User, but should work with other user models as well.
    """

    user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True, on_delete=models.CASCADE)

    net_number = models.PositiveSmallIntegerField(null=True, unique=True)
    informal_email = models.EmailField(_('Informal email address'))

    image = ThumbnailImageField(upload_to=_gen_image_name, blank=True)