from django.db import models
from product.models import Product, CommonInfo
from services.models import Service
from django.utils import timezone
from django.contrib.auth.hashers import make_password
from PIL import Image

from django.db import models
import django.utils.timezone
from django.utils.translation import ugettext_lazy as _


class Customer(CommonInfo):
    customer_code = models.CharField(max_length=255, verbose_name=_("Code"))
    customer_name = models.CharField(max_length=512, verbose_name=_("Nom"))
    customer_phone = models.CharField(
        max_length=255, null=True, blank=True, verbose_name=_("Téléphone"))
    customer_password = models.CharField(
        max_length=255, default=make_password('0000'))
    customer_email = models.CharField(
        max_length=255, null=True, blank=True, verbose_name=_("Email"))
    customer_address = models.CharField(
        max_length=255, null=True, blank=True, verbose_name=_("Adresse"))
    products = models.ManyToManyField(Product)
    services = models.ManyToManyField(Service)

    def __str__(self):
        return self.customer_name


class UserProfile(models.Model):
    userprofile_isforpartner = models.BooleanField(default=False)
    userprofile_isforilt = models.BooleanField(default=False)
    customer = models.ForeignKey(
        Customer, on_delete=models.CASCADE, related_name='custumer_profiles', null=True)
    image = models.ImageField(
        default='profile_pics/default.jpeg', upload_to='profile_pics')

    def __str__(self):
        return self.customer.customer_name

    def save(self, *args, **kwargs):
        super().save()
        img = Image.open(self.image.path)
        if img.height > 100 or img.width > 100:
            output_size = (100, 100)
            img.thumbnail(output_size)
            img.save(self.image.path)


class Testimony(models.Model):
    testimony_code = models.CharField(unique=True, max_length=255)
    testimony_content = models.TextField(null=False)
    testimony_posteddate = models.DateTimeField(default=timezone.now)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    testimony_isvalidated = models.BooleanField(default=False)

    class Meta:
        ordering = ['-testimony_posteddate']
