from django.db import models
from django.core.validators import FileExtensionValidator
from profiles.models import Profile
from sorl.thumbnail import ImageField

class PublishManager(models.Manager):
  def get_queryset(self):
    queryset = super().get_queryset().filter(draft=False)
    return queryset

class Post(models.Model):
  content = models.TextField()
  image   = ImageField(upload_to='posts/', validators=[FileExtensionValidator(['png', 'jpg', 'jpeg'])], blank=True)
  liked   = models.ManyToManyField(Profile, blank=True, related_name='likes')
  updated = models.DateTimeField(auto_now=True)
  created = models.DateTimeField(auto_now_add=True)
  author  = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='posts')
  draft   = models.BooleanField(default=False)

  objects = models.Manager()
  published = PublishManager()

  def __str__(self):
    return str(self.content[:20])

  def num_likes(self):
    return self.liked.all().count()
  
  def num_comments(self):
    return self.comments.all().count()

  class Meta:
    ordering = ('-created', )
  

class Comment(models.Model):
  user    = models.ForeignKey(Profile, on_delete=models.CASCADE)
  post    = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
  body    = models.TextField(max_length=300)
  updated = models.DateTimeField(auto_now=True)
  created = models.DateTimeField(auto_now_add=True)

  def __str__(self):
    return f"comment on post:'{self.post}' by '{self.user}' -- {self.pk}"

LIKE_CHOICES = (
  ('Like', 'Like'),
  ('Unlike', 'Unlike'),
)

class Like(models.Model):
  user    = models.ForeignKey(Profile, on_delete=models.CASCADE)
  post    = models.ForeignKey(Post, on_delete=models.CASCADE)
  value   = models.CharField(choices=LIKE_CHOICES, max_length=8)
  updated = models.DateTimeField(auto_now=True)
  created = models.DateTimeField(auto_now_add=True)

  def __str__(self):
    return f"{self.user}-{self.post}-{self.value}"
