from django.db import models
from django.utils import timezone
from django.urls import reverse

class Post(models.Model):
    """ topics model """
    
    #superuser
    author = models.ForeignKey('auth.User',on_delete=models.CASCADE)

    title = models.CharField(max_length =200)
    text = models.TextField()
    create_date = models.DateTimeField(default=timezone.now)
    publish_date = models.DateTimeField(blank=True, null=True)

    def publish(self):
        """ post the date of publish """

        self.publish_date = timezone.now()
        self.save()

    def approve_comments(self):
        """ return approved comments """

        return self.kommente.filter(approved_comment = True)

    def get_absolute_url(self):
        return reverse("blog:post_detail",kwargs={'pk':self.pk})

    def __str__(self):
        return self.title



class Comment(models.Model):
    """ comments model """

    post = models.ForeignKey('blog.Post',related_name= 'kommente',on_delete = models.CASCADE)
    author = models.CharField(max_length=200)
    text = models.TextField()
    create_date = models.DateTimeField(default=timezone.now)
    approved_comment = models.BooleanField(default = False)

    def approve(self):
        self.approved_comment = True
        self.save()

    def get_absolute_url(self):
        return reverse("blog:post_list")

    def __str__(self):
        return self.text
