from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from .base import Base, BaseMixin


class Article(Base, BaseMixin):
    __tablename__ = 'article'
    title = Column(String(length=255), unique=True)
    article_authors = relationship("ArticleAuthor")


class Author(Base, BaseMixin):
    __tablename__ = 'author'
    first_name = Column(String(255))
    last_name = Column(String(255))
    article_authors = relationship("ArticleAuthor")
    book_authors = relationship("BookAuthor")


class BookshelfPublisher(Base, BaseMixin):
    __tablename__ = 'bookshelf_publisher'
    name = Column(String(length=255), unique=True)
    books = relationship("Book")


class Book(Base, BaseMixin):
    __tablename__ = 'book'
    isbn = Column(String(255), unique=True)
    title = Column(String(255))
    year = Column(Integer, nullable=False)
    publisher_id = Column(String, ForeignKey('bookshelf_publisher.id'), nullable=False)
    publisher = relationship('BookshelfPublisher', back_populates="books")
    book_authors = relationship("BookAuthor")


class ArticleAuthor(Base, BaseMixin):
    __tablename__ = 'article_author'
    article_id = Column(String, ForeignKey('article.id'), nullable=False)
    article = relationship('Article', back_populates="article_authors")
    author_id = Column(String, ForeignKey('author.id'), nullable=False)
    author = relationship('Author', back_populates="article_authors")


class BookAuthor(Base, BaseMixin):
    __tablename__ = 'book_author'
    author_id = Column(String, ForeignKey('author.id'), nullable=False)
    author = relationship('Author', back_populates="book_authors")
    book_id = Column(String, ForeignKey('book.id'), nullable=False)
    book = relationship('Book', back_populates="book_authors")
