"""Country, League, Season models (football schema)

We keep the filename `sport.py` to minimize project churn, but the
project is currently focused on football only.
"""

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

from app.core.db.database import Base


class Country(Base):
    """Country dictionary"""

    __tablename__ = "countries"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, unique=True, nullable=False, index=True)
    code = Column(String)

    leagues = relationship("League", back_populates="country")
    teams = relationship("Team", back_populates="country")


class League(Base):
    """League/competition"""

    __tablename__ = "leagues"

    id = Column(Integer, primary_key=True, index=True)
    country_id = Column(Integer, ForeignKey("countries.id"), nullable=True)
    name = Column(String, nullable=False, index=True)
    level = Column(Integer)

    # External IDs (Flashscore etc.)
    source_id = Column(String, index=True)
    code = Column(String, unique=True, index=True)

    country = relationship("Country", back_populates="leagues")
    seasons = relationship("Season", back_populates="league")
    matches = relationship("Match", back_populates="league")


class Season(Base):
    """Season within a league"""

    __tablename__ = "seasons"

    id = Column(Integer, primary_key=True, index=True)
    league_id = Column(Integer, ForeignKey("leagues.id"), nullable=False)
    name = Column(String, nullable=False, index=True)

    # In our DB schema these may be stored as date_start/date_end.
    # We map to start_date/end_date columns that we will add via SQL patch.
    start_date = Column(Date)
    end_date = Column(Date)
    is_current = Column(Integer, default=1)  # 1 = current, 0 = past

    league = relationship("League", back_populates="seasons")
    matches = relationship("Match", back_populates="season")
