import os

from django.urls import reverse, NoReverseMatch
from django.db import models
from django.utils.timezone import now as timezone_now
from django.utils.translation import ugettext_lazy as _

from utils.models import UrlMixin


def product_photo_upload_to(instance, filename):
    now = timezone_now()
    slug = instance.product.slug
    base, ext = os.path.splitext(filename)
    return f"products/{slug}/{now:%Y%m%d%H%M%S}{ext.lower()}"


class Product(UrlMixin):
    class Meta:
        verbose_name = _("Product")
        verbose_name_plural = _("Products")

    title = models.CharField(_("title"),
                             max_length=200)
    slug = models.SlugField(_("slug"),
                            max_length=200)
    description = models.TextField(_("description"),
                                   blank=True)
    price = models.DecimalField(_("price (USD)"),
                                max_digits=8,
                                decimal_places=2,
                                blank=True,
                                null=True)

    def get_url_path(self):
        try:
            return reverse("product_detail",
                           kwargs={"slug": self.slug})
        except NoReverseMatch:
            return ""

    def __str__(self):
        return self.title


class ProductPhoto(models.Model):
    class Meta:
        verbose_name = _("Photo")
        verbose_name_plural = _("Photos")

    product = models.ForeignKey(Product,
                                on_delete=models.CASCADE)
    photo = models.ImageField(_("photo"),
                              upload_to=product_photo_upload_to)

    def __str__(self):
        return self.photo.name
