from __future__ import annotations

from django.db import models


class Author(models.Model):
    """A writer of books."""

    name = models.CharField(max_length=100, help_text="The author's full name.")
    email = models.EmailField(help_text="Contact email address.")
    bio = models.TextField(blank=True, help_text="Short biography.")
    is_active = models.BooleanField(
        default=True, help_text="Whether the author is active."
    )
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        app_label = "tests"
        verbose_name = "author"
        verbose_name_plural = "authors"

    def __str__(self) -> str:
        return self.name


class Book(models.Model):
    """A published book."""

    class Genre(models.TextChoices):
        FICTION = "fiction", "Fiction"
        NON_FICTION = "non_fiction", "Non-Fiction"
        SCIENCE = "science", "Science"

    title = models.CharField(max_length=200, help_text="Book title.")
    author = models.ForeignKey(
        Author,
        on_delete=models.CASCADE,
        related_name="books",
        help_text="The author who wrote this book.",
    )
    genre = models.CharField(
        max_length=20,
        choices=Genre.choices,
        default=Genre.FICTION,
        help_text="Literary genre.",
    )
    published_year = models.PositiveIntegerField(
        null=True, blank=True, help_text="Year of publication."
    )
    rating = models.FloatField(default=0.0, help_text="Average reader rating.")

    class Meta:
        app_label = "tests"
        verbose_name = "book"
        verbose_name_plural = "books"

    def __str__(self) -> str:
        return self.title
