from django.db import models
from django.utils.text import slugify
from django.db.models.signals import pre_save, m2m_changed
from django.dispatch import receiver
from django.urls import reverse
from django.contrib.auth import get_user_model

# Create your models here.
User = get_user_model()


class Category(models.Model):
    name = models.CharField(max_length=30)
    slug = models.SlugField(blank=True, allow_unicode=True)

    def __str__(self):
        return self.name


class Article(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE,
                               blank=True, null=True,
                               related_name='articlesOfUser')
    author_name = models.CharField(max_length=225, blank=True)
    title = models.CharField(max_length=30)
    created_at = models.DateTimeField(auto_now_add=True)
    featured = models.BooleanField(default=False)
    category = models.ManyToManyField(
        Category, related_name="categoriesOfArticles")
    content = models.TextField()
    picture = models.ImageField(upload_to='article_pictures', blank=True)
    slug = models.SlugField(blank=True, allow_unicode=True)
    fans = models.ManyToManyField(User, related_name='fav_articles')
    fans_number = models.PositiveIntegerField(default=0)
    commenters = models.ManyToManyField(User, through='Comments')

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('articles:article', args=[self.slug])

    def image_url(self):
        if self.picture and hasattr(self.picture, 'url'):
            return self.picture.url
        else:
            return "/static/img.png"


class Comments(models.Model):
    article = models.ForeignKey(Article,on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    text = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f'commenter: {self.user.username} / article: {self.article.title}'



def create_slug(instance, Model, field_name, new_slug=None):
    slug = new_slug or slugify(
        getattr(instance, field_name), allow_unicode=True)
    qs = Model.objects.filter(slug=slug).order_by('-id')
    if qs.exists():
        new_slug = f'{slug}-{qs.first().id}'
        return create_slug(instance, Model, field_name, new_slug=new_slug)
    return slug


@receiver(m2m_changed, sender=Article.fans.through)
def update_fans_number(sender, instance, **kwargs):
    instance.fans_number = instance.fans.all().count()
    instance.save()

@receiver(pre_save, sender=Category)
def add_slug_to_Category(sender, instance, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance, sender, 'name')


@receiver(pre_save, sender=Article)
def add_slug_to_Article(sender, instance, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance, sender, 'title')


@receiver(pre_save, sender=Article)
def add_author_name_to_article(sender, instance, **kwargs):
    author_name = "staff"
    author = instance.author
    if author:
        author_name = author.name()
    instance.author_name = author_name
