from django.utils import timezone
from django.conf import settings
from django.db.models import (
    Model,
    CharField,
    BooleanField,
    DateTimeField,
    IntegerField,
    FilePathField,
    ForeignKey,
    CASCADE
)


def get_gallery_root():
    return settings.GALLERY_ROOT


class UserSettings(Model):
    """
    User relations to a specific image file by path.
    """

    user = ForeignKey(settings.AUTH_USER_MODEL, blank=False, null=False, on_delete=CASCADE)
    theme = CharField(max_length=5, blank=False, null=False, default='dark')
    sort = CharField(max_length=6, blank=False, null=False, default='abc')

    class meta:
        ordering = ["pk"]


class Image(Model):
    """
    User relations to a specific image file by path.
    """

    user = ForeignKey(settings.AUTH_USER_MODEL, blank=False, null=False, on_delete=CASCADE)
    path = FilePathField(path=get_gallery_root, blank=False, null=False)
    favorite = BooleanField(blank=False, null=False, default=False)
    last_visited = DateTimeField(blank=False, null=False, default=timezone.now)
    visits = IntegerField(blank=False, null=False, default=0)

    class meta:
        ordering = ["pk"]
        get_latest_by = "-last_visited"
