from django.conf import settings
from django.db import models
from django.urls import reverse
from django.utils.text import slugify
from django.utils import timezone
from django.contrib.auth.models import User


class Article(models.Model):
    """ Represents a single wiki article. """
    title = models.CharField(max_length=settings.WIKI_ARTICLE_TITLE_MAX_LENGTH, unique=True,
                             help_text="Title of your article.")
    author = models.ForeignKey(User, on_delete=models.PROTECT,
                               help_text="The user that posted this article.")
    slug = models.CharField(max_length=settings.WIKI_ARTICLE_TITLE_MAX_LENGTH, blank=True, editable=False,
                            help_text="Unique URL path to access this article. Generated by the system.")
    content = models.TextField(
        help_text="Write the content of your particleage here.")
    created = models.DateTimeField(auto_now_add=True,
                                   help_text="The date and time this article was created. Automatically generated when the model saves.")
    modified = models.DateTimeField(auto_now=True,
                                    help_text="The date and time this article was updated. Automatically generated when the model updates.")

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        """ Returns a fully-qualified path for a article (/my-new-wiki-article). """
        path_components = {'slug': self.slug}
        return reverse('wiki-details-article', kwargs=path_components)

    def save(self, *args, **kwargs):
        """ Creates a URL safe slug automatically when a new a article is created. """
        if not self.pk:
            self.slug = slugify(self.title, allow_unicode=True)

        # Call save on the superclass.
        return super(Article, self).save(*args, **kwargs)
