from __future__ import unicode_literals

from django.db import models


class BaseInfo(models.Model):
    """Base class containing all models common information."""

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        """Define Model as abstract."""

        abstract = True


class User(BaseInfo):
    """User model defined."""

    USER_ROLE = (
        ('A', 'Admin'),
        ('U', 'User'),
    )

    name = models.CharField(max_length=200)
    email = models.EmailField(max_length=254)
    role = models.CharField(max_length=2, choices=USER_ROLE)


class Author(BaseInfo):
    """Book author models defined."""

    name = models.CharField(max_length=200)


class Book(BaseInfo):
    """Book model defined."""

    title = models.CharField(max_length=100)
    description = models.TextField()
    quantity = models.IntegerField()
    edition = models.CharField(max_length=100)
    publisher = models.CharField(max_length=100)
    isbn = models.CharField(max_length=50)
    author = models.ForeignKey('Author', on_delete=models.CASCADE)

    def _check_status(self):
        if self.quantity <= 0:
            return "Not Available"
        else:
            return "Available"
    status = property(_check_status)


class Review(BaseInfo):
    """Book review model defined."""

    comment = models.TextField()
    # ratings needs to be implemented here.
    user = models.ForeignKey('User', on_delete=models.CASCADE)
    book = models.ForeignKey('Book', on_delete=models.CASCADE)


class History(BaseInfo):
    """User history model defined."""

    lending_date = models.DateTimeField(auto_now_add=True)
    return_date = models.DateTimeField(auto_now_add=False,
                                       auto_now=False,
                                       blank=True, null=True)
    returned = models.BooleanField(default=False)

    def _expected_return_date(self):
        import datetime
        present_day = datetime.date.today()
        day_14 = datetime.timedelta(weeks=2)
        return_date = present_day + day_14
        return return_date

    exptdreturn_date = property(_expected_return_date)

    book = models.ForeignKey('Book', on_delete=models.CASCADE)
    user = models.ForeignKey('User', on_delete=models.CASCADE)

@@ -739,16 +739,83 @@
 h=200)%0A%0A
+    def __unicode__(self):%0A        return %22%7B%7D%22 .format(self.name)%0A%0A
 %0Aclass B
@@ -1364,16 +1364,96 @@
 tatus)%0A%0A
+    def __unicode__(self):%0A        return %22Book title: %7B%7D%22 .format(self.title)%0A%0A
 %0Aclass R
@@ -1707,24 +1707,108 @@
 s.CASCADE)%0A%0A
+    def __unicode__(self):%0A        return %22Review by user : %7B%7D%22 .format(self.user)%0A%0A
 %0Aclass Histo
@@ -2482,24 +2482,24 @@
 ls.CASCADE)%0A
-
     user = m
@@ -2529,28 +2529,114 @@
 , on_delete=models.CASCADE)%0A
+%0A    def __unicode__(self):%0A        return %22History for user : %7B%7D%22 .format(self.user)%0A

add unicode definition for models