import click

from sqlalchemy import CheckConstraint, Column, ForeignKey, Index, Integer
from sqlalchemy.orm import relationship

from .show import Base, Show


class ShiftedSeason(Base):

    __tablename__ = 'shifted_seasons'
    __table_args__ = (
        CheckConstraint(
            "(show_id IS NOT NULL AND pattern_id IS NULL) OR (show_id IS NULL AND pattern_id IS NOT NULL)",
            name="ck_shifted_seasons_single_owner",
        ),
        Index("ix_shifted_seasons_show_id", "show_id"),
        Index("ix_shifted_seasons_pattern_id", "pattern_id"),
    )

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


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

    # v1.x
    show_id = Column(Integer, ForeignKey('shows.id', ondelete="CASCADE"), nullable=True)
    show = relationship(Show, back_populates='shifted_seasons', lazy='joined')

    pattern_id = Column(Integer, ForeignKey('patterns.id', ondelete="CASCADE"), nullable=True)
    pattern = relationship('Pattern', back_populates='shifted_seasons', lazy='joined')

    # v2.0
    # show_id: Mapped[int] = mapped_column(ForeignKey("shows.id", ondelete="CASCADE"))
    # show: Mapped["Show"] = relationship(back_populates="patterns")


    original_season = Column(Integer)

    first_episode = Column(Integer, default = -1)
    last_episode = Column(Integer, default = -1)

    season_offset = Column(Integer, default = 0)
    episode_offset = Column(Integer, default = 0)


    def getId(self):
        return self.id

    def getShowId(self):
        return self.show_id

    def getPatternId(self):
        return self.pattern_id


    def getOriginalSeason(self):
        return self.original_season

    def getFirstEpisode(self):
        return self.first_episode

    def getLastEpisode(self):
        return self.last_episode


    def getSeasonOffset(self):
        return self.season_offset
    
    def getEpisodeOffset(self):
        return self.episode_offset


    def getObj(self):

        shiftedSeasonObj = {}

        shiftedSeasonObj['show_id'] = self.getShowId()
        shiftedSeasonObj['pattern_id'] = self.getPatternId()
        shiftedSeasonObj['original_season'] = self.getOriginalSeason()
        shiftedSeasonObj['first_episode'] = self.getFirstEpisode()
        shiftedSeasonObj['last_episode'] = self.getLastEpisode()
        shiftedSeasonObj['season_offset'] = self.getSeasonOffset()
        shiftedSeasonObj['episode_offset'] = self.getEpisodeOffset()

        return shiftedSeasonObj
