# from typing import List
from sqlalchemy import create_engine, Column, Integer, String, Text, ForeignKey
from sqlalchemy.orm import relationship, declarative_base, sessionmaker

from ffx.show_descriptor import ShowDescriptor

Base = declarative_base()


class Show(Base):
    """
    relationship(argument, opt1, opt2, ...)
    argument is string of class or Mapped class of the target entity
    backref creates a bi-directional corresponding relationship (back_populates preferred)
    back_populates points to the corresponding relationship (the actual class attribute identifier)
    
    See: https://docs.sqlalchemy.org/en/(14|20)/orm/basic_relationships.html
    """

    __tablename__ = 'shows'

    # v1.x
    id = Column(Integer, primary_key=True)

    name = Column(String)
    year = Column(Integer)

    # v2.0
    # id: Mapped[int] = mapped_column(Integer, primary_key=True)
    # name: Mapped[str] = mapped_column(String, nullable=False)
    # year: Mapped[int] = mapped_column(Integer, nullable=False)

    # v1.x
    #patterns = relationship('Pattern', back_populates='show', cascade="all, delete", passive_deletes=True)
    patterns = relationship('Pattern', back_populates='show', cascade="all, delete")
    # patterns = relationship('Pattern', back_populates='show', cascade="all")

    # v2.0
    # patterns: Mapped[List["Pattern"]] = relationship(back_populates="show", cascade="all, delete")

    shifted_seasons = relationship('ShiftedSeason', back_populates='show', cascade="all, delete")


    index_season_digits = Column(Integer, default=ShowDescriptor.DEFAULT_INDEX_SEASON_DIGITS)
    index_episode_digits = Column(Integer, default=ShowDescriptor.DEFAULT_INDEX_EPISODE_DIGITS)
    indicator_season_digits = Column(Integer, default=ShowDescriptor.DEFAULT_INDICATOR_SEASON_DIGITS)
    indicator_episode_digits = Column(Integer, default=ShowDescriptor.DEFAULT_INDICATOR_EPISODE_DIGITS)
    quality = Column(Integer, default=0)
    notes = Column(Text, default='')


    def getDescriptor(self, context):
        
        kwargs = {}
        kwargs[ShowDescriptor.CONTEXT_KEY] = context
        kwargs[ShowDescriptor.ID_KEY] = int(self.id)
        kwargs[ShowDescriptor.NAME_KEY] = str(self.name)
        kwargs[ShowDescriptor.YEAR_KEY] = int(self.year)
        kwargs[ShowDescriptor.INDEX_SEASON_DIGITS_KEY] = int(self.index_season_digits)
        kwargs[ShowDescriptor.INDEX_EPISODE_DIGITS_KEY] = int(self.index_episode_digits)
        kwargs[ShowDescriptor.INDICATOR_SEASON_DIGITS_KEY] = int(self.indicator_season_digits)
        kwargs[ShowDescriptor.INDICATOR_EPISODE_DIGITS_KEY] = int(self.indicator_episode_digits)
        kwargs[ShowDescriptor.QUALITY_KEY] = int(self.quality or 0)
        kwargs[ShowDescriptor.NOTES_KEY] = str(self.notes or '')
        
        return ShowDescriptor(**kwargs)
