from django.db import models
from django.utils.translation import gettext_lazy as _

from apps.shared.models import AbstractBaseModel


class Photo(AbstractBaseModel):
    name = models.CharField(
        max_length=255, blank=True, null=True, verbose_name=_("Nomi")
    )
    description = models.TextField(blank=True, null=True, verbose_name=_("Tavsif"))
    image = models.ImageField(upload_to="media/", verbose_name=_("Fayl"))
    type = models.CharField(
        max_length=255, blank=True, null=True, verbose_name=_("Fayl turi")
    )
    size = models.BigIntegerField(
        blank=True, null=True, default=0, verbose_name=_("Hajmi")
    )

    def save(self, *args, **kwargs):
        self.type = self.image.name.split(".")[-1]
        self.size = self.image.size
        if self.name is None:
            self.name = (
                self.image.name if self.image.name is not None else f"Media {self.id}"
            )
        super().save(*args, **kwargs)

    class Meta:
        verbose_name = _("Барча кўрнишлар")
        verbose_name_plural = _("Барча кўрнишлар")
        ordering = ["-created_at"]

    def __str__(self) -> str:
        return str(self.description) if str(self.description) else f"Photo {self.id}"


class Gallery(AbstractBaseModel):
    name = models.CharField(
        max_length=255, blank=True, null=True, verbose_name=_("Nomi")
    )
    description = models.TextField(blank=True, null=True, verbose_name=_("Tavsif"))
    image = models.ImageField(upload_to="media/", verbose_name=_("Fayl"))
    type = models.CharField(
        max_length=255, blank=True, null=True, verbose_name=_("Fayl turi")
    )
    size = models.BigIntegerField(
        blank=True, null=True, default=0, verbose_name=_("Hajmi")
    )

    def save(self, *args, **kwargs):
        self.type = self.image.name.split(".")[-1]
        self.size = self.image.size
        if self.name is None:
            self.name = (
                self.image.name if self.image.name is not None else f"Media {self.id}"
            )
        super().save(*args, **kwargs)

    class Meta:
        verbose_name = _("Galereya")
        verbose_name_plural = _("Galereya")
        ordering = ["-created_at"]

    def __str__(self) -> str:
        return str(self.description) if str(self.description) else f"Gallery {self.id}"
