from django.db import models
from django.conf import settings
from django.utils.text import slugify
from django.contrib.auth.models import User
# Create your models here.
class Book(models.Model):
    title = models.CharField(max_length = 200, default='book title', null=True)
    summary = models.TextField(default='book description')
    slug = models.CharField(max_length=300, blank=True, editable=False,
                            help_text="Unique URL path to access this book. Generated by the system.")
    # author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
    author = models.CharField(max_length = 200, default='book author', null=True)
    isbn = models.CharField('ISBN', max_length=13,default='0000', help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>')
    image = models.CharField(max_length = 400, default='book image url', null=True)

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

    def save(self, *args, **kwargs):
        """ Creates a URL safe slug automatically when a new a page is created. """
        if not self.pk:
            self.slug = slugify(self.title, allow_unicode=True)
        # Call save on the superclass.
        return super(Book, self).save(*args, **kwargs)
    
    # title = models.CharField(max_length=25, default='book title')

    # def _str_(self): #show the actual city name on the dashboard
    #     return self.title

class Comment(models.Model):
    RATING_RANGE = (
        (1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
        (5, '5')
    )
    comment = models.TextField(default='book comment')
    author = models.ForeignKey(User, on_delete=models.PROTECT,
                               help_text="The user that posted this book.", null=True)
    rating = models.IntegerField(choices=RATING_RANGE, default=RATING_RANGE[0][0])
    book = models.ForeignKey(Book, on_delete = models.CASCADE)
    def __str__(self):
        return self.comment
        