from django.db import models
from django.contrib.auth.models import User
from datetime import date
# Create your models here.

class Genre(models.Model):
    '''Model represesnting the a book genre'''

    name = models.CharField(max_length=200,help_text="Enter the book genre(Eg:Science friction,French etc)")

    def __str__(self):
        '''String for rep the genre(from admin site)'''
        return self.name

from django.urls import reverse

class Books(models.Model):
    '''model rep a book'''

    title = models.CharField(max_length=200)
    author = models.ForeignKey('Author',on_delete=models.SET_NULL,null=True)
    #Foreign key used because books can have only one author'''
    ##author as string raher than object

    summary = models.TextField(max_length=200,help_text="Enter brief description of the Book")
    isbn = models.CharField('ISBN',max_length=13,help_text='13 Character <a href ="https://www.isbn-international.org/content/what-is-ISBN">ISBN number</a>')
    genre = models.ManyToManyField(Genre,help_text="Select a Genre for this book")
    # M2M becoz genre can contain many books , as well as books can contain many genre

    def __str__(self):
    #string for representing Model object
        return self.title

    def get_absolute_url(self):
        '''return a url to access a detail of this book'''
        return reverse('Book-detail',args=[str(self.id)])

    def display_genre(self):
        return ','.join([genre.name for genre in self.genre.all()[:3]])
    display_genre.short_description ='Genre'

import uuid

class BookInstance(models.Model):
    '''Models repr copies of the books'''
    id = models.UUIDField(primary_key=True,default= uuid.uuid4,help_text="Information to be displayed for uiid for books")
    book = models.ForeignKey('Books',on_delete=models.SET_NULL,null=True)
    imprint = models.CharField(max_length=200)
    due_back = models.DateField(null=True,blank=True)
    borrower=models.ForeignKey(User,on_delete=models.SET_NULL,null=True,blank=True)

    LOANS_STATUS =(
        ('m','Maintenance'),
        ('o','ON Loan'),
        ('a','Available'),
        ('r','Reserved')
    )

    status = models.CharField(max_length=1,choices=LOANS_STATUS,blank = True,default='m',help_text="Availability of book")

    class Meta:
        ordering = ["due_back"]
        permissions = (("can_mark_returned", "Set book as returned"),)

    def __str__(self):
        '''String for representing the model obj'''
        return '{0}({1})'.format(self.id,self.book.title)
    @property
    def is_overdue(self):
        if self.due_back and date.today()>self.due_back:
            return True
        return False


class Author(models.Model):
    """
    Model representing an author.
    """
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    date_of_birth = models.DateField(null=True, blank=True)
    date_of_death = models.DateField('Died', null=True, blank=True)

    class Meta:
        ordering = ["last_name", "first_name"]

    def get_absolute_url(self):
        """
        Returns the url to access a particular author instance.
        """
        return reverse('author-detail', args=[str(self.id)])

    def __str__(self):
        """
        String for representing the Model object.
        """
        return '{0}, {1}'.format(self.last_name, self.first_name)



