from django.db import models
from django.contrib import auth

# Create your models here.
class Publisher(models.Model):
    """
    A company that publishes books.
    """

    name = models.CharField(max_length=50, help_text='The name of publisher')
    website = models.URLField(help_text="The publisher's website")
    email = models.EmailField(help_text="The publisher's email address")

    def __str__(self):

        return self.name

class Book(models.Model):
    """
    Contains books descriptions
    """

    title = models.CharField(max_length=70, help_text="The title of the book")
    publication_date = models.DateField(verbose_name="Date the book was published")
    isbn = models.CharField(max_length=20, verbose_name="ISBN number of the book")
    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
    contributors = models.ManyToManyField('Contributor', through='BookContributor')

    def __str__(self):

        return self.title

class Contributor(models.Model):
    """
    Information about contributors (authors, co-authors and so on).
    """

    first_names = models.CharField(
        max_length=50,
        help_text="The contributor's first name or names"
    )
    last_names = models.CharField(
        max_length=50,
        help_text = "The contributor's last name or names"
    )
    email = models.EmailField(help_text='The contact email for the contributor.')

    def __str__(self):

        return self.first_names

class BookContributor(models.Model):

    class ContributionRole(models.TextChoices):

        AUTHOR = "AUTHOR", "Author"
        CO_AUTHOR = "CO-AUTHOR", "Co-Author"
        EDITOR = "EDITOR", "Editor"

    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    contributor = models.ForeignKey(Contributor, on_delete=models.CASCADE)
    role = models.CharField(
        verbose_name="The role this contributor had in this book.",
        choices=ContributionRole.choices,
        max_length=20
    )

class Review(models.Model):
    """
    Define reviews for books given by users
    """

    content = models.TextField(
        help_text="The review text."
    )
    rating = models.IntegerField(
        help_text="The rating the reviewer was given."
    )
    date_created = models.DateTimeField(
        auto_now_add=True,
        help_text="The date and time review was given."
    )
    date_edited = models.DateTimeField(
        null=True,
        help_text="The date and time the review was last edited."
    )
    creator = models.ForeignKey(
        auth.get_user_model(),
        on_delete=models.CASCADE
    )
    book = models.ForeignKey(
        Book, 
        on_delete=models.CASCADE,
        help_text="The Book that this review is for"
    )