# Path: nflpool/data/dbsession.py
# class DbSessionFactory:
#     factory = None
# 
#     # Start a database session at app startup
#     @staticmethod
#     def global_init(db_file):
#         if DbSessionFactory.factory:
#             return
# 
#         if not db_file or not db_file.strip():
#             raise Exception("You must specify a data file.")
# 
#         conn_str = "sqlite:///" + db_file
#         print("Connecting to db with conn string: {}".format(conn_str))
# 
#         engine = sqlalchemy.create_engine(conn_str, echo=False)
# 
#         SqlAlchemyBase.metadata.create_all(engine)
#         DbSessionFactory.factory = sqlalchemy.orm.sessionmaker(bind=engine)
# 
#     @staticmethod
#     def create_session():
#         return DbSessionFactory.factory()
# 
# Path: nflpool/data/divisioninfo.py
# class DivisionInfo(SqlAlchemyBase):
#     __tablename__ = "DivisionInfo"
#     division_id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
#     division = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/data/conferenceinfo.py
# class ConferenceInfo(SqlAlchemyBase):
#     __tablename__ = "ConferenceInfo"
#     conf_id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
#     conference = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/data/picktypes.py
# class PickTypes(SqlAlchemyBase):
#     __tablename__ = "PickTypes"
#     pick_type_id = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     name = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/data/weekly_nflplayer_stats.py
# class WeeklyNFLPlayerStats(SqlAlchemyBase):
#     __tablename__ = "WeeklyNFLPlayerStats"
#     primary_key = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     season = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     week = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     player_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("ActiveNFLPlayers.player_id")
#     )
#     passyds = sqlalchemy.Column(sqlalchemy.Integer)
#     rushyds = sqlalchemy.Column(sqlalchemy.Integer)
#     recyds = sqlalchemy.Column(sqlalchemy.Integer)
#     sacks = sqlalchemy.Column(sqlalchemy.REAL)
#     interceptions = sqlalchemy.Column(sqlalchemy.Integer)
# 
# Path: nflpool/data/activeplayers.py
# class ActiveNFLPlayers(SqlAlchemyBase):
#     __tablename__ = "ActiveNFLPlayers"
#     primary_key = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True, index=True
#     )
#     season = sqlalchemy.Column(sqlalchemy.Integer)
#     team_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("TeamInfo.team_id")
#     )
#     firstname = sqlalchemy.Column(sqlalchemy.String)
#     lastname = sqlalchemy.Column(sqlalchemy.String)
#     position = sqlalchemy.Column(sqlalchemy.String, index=True)
#     player_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("PlayerPicks.player_id"), index=True
#     )
# 
# Path: nflpool/data/teaminfo.py
# class TeamInfo(SqlAlchemyBase):
#     __tablename__ = "TeamInfo"
#     team_id = sqlalchemy.Column(sqlalchemy.Integer, index=True, primary_key=True)
#     name = sqlalchemy.Column(sqlalchemy.String)
#     city = sqlalchemy.Column(sqlalchemy.String)
#     team_abbr = sqlalchemy.Column(sqlalchemy.String)
#     conference_id = sqlalchemy.Column(sqlalchemy.Integer)
#     division_id = sqlalchemy.Column(sqlalchemy.Integer)


from nflpool.data.dbsession import DbSessionFactory
from nflpool.data.divisioninfo import DivisionInfo
from nflpool.data.conferenceinfo import ConferenceInfo
from nflpool.data.picktypes import PickTypes
from nflpool.data.weekly_nflplayer_stats import WeeklyNFLPlayerStats
from nflpool.data.activeplayers import ActiveNFLPlayers
from nflpool.data.teaminfo import TeamInfo
from itertools import groupby


def interception_leaders():
    session = DbSessionFactory.create_session()

    afc_interception_query = (
        session.query(
            WeeklyNFLPlayerStats.interceptions, WeeklyNFLPlayerStats.player_id
        )
        .join(ActiveNFLPlayers)
        .outerjoin(TeamInfo, ActiveNFLPlayers.team_id == TeamInfo.team_id)
        .filter(TeamInfo.conference_id == 0)

# Path: nflpool/data/dbsession.py
# class DbSessionFactory:
#     factory = None
# 
#     # Start a database session at app startup
#     @staticmethod
#     def global_init(db_file):
#         if DbSessionFactory.factory:
#             return
# 
#         if not db_file or not db_file.strip():
#             raise Exception("You must specify a data file.")
# 
#         conn_str = "sqlite:///" + db_file
#         print("Connecting to db with conn string: {}".format(conn_str))
# 
#         engine = sqlalchemy.create_engine(conn_str, echo=False)
# 
#         SqlAlchemyBase.metadata.create_all(engine)
#         DbSessionFactory.factory = sqlalchemy.orm.sessionmaker(bind=engine)
# 
#     @staticmethod
#     def create_session():
#         return DbSessionFactory.factory()
# 
# Path: nflpool/data/divisioninfo.py
# class DivisionInfo(SqlAlchemyBase):
#     __tablename__ = "DivisionInfo"
#     division_id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
#     division = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/data/conferenceinfo.py
# class ConferenceInfo(SqlAlchemyBase):
#     __tablename__ = "ConferenceInfo"
#     conf_id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
#     conference = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/data/picktypes.py
# class PickTypes(SqlAlchemyBase):
#     __tablename__ = "PickTypes"
#     pick_type_id = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     name = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/data/weekly_nflplayer_stats.py
# class WeeklyNFLPlayerStats(SqlAlchemyBase):
#     __tablename__ = "WeeklyNFLPlayerStats"
#     primary_key = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     season = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     week = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     player_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("ActiveNFLPlayers.player_id")
#     )
#     passyds = sqlalchemy.Column(sqlalchemy.Integer)
#     rushyds = sqlalchemy.Column(sqlalchemy.Integer)
#     recyds = sqlalchemy.Column(sqlalchemy.Integer)
#     sacks = sqlalchemy.Column(sqlalchemy.REAL)
#     interceptions = sqlalchemy.Column(sqlalchemy.Integer)
# 
# Path: nflpool/data/activeplayers.py
# class ActiveNFLPlayers(SqlAlchemyBase):
#     __tablename__ = "ActiveNFLPlayers"
#     primary_key = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True, index=True
#     )
#     season = sqlalchemy.Column(sqlalchemy.Integer)
#     team_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("TeamInfo.team_id")
#     )
#     firstname = sqlalchemy.Column(sqlalchemy.String)
#     lastname = sqlalchemy.Column(sqlalchemy.String)
#     position = sqlalchemy.Column(sqlalchemy.String, index=True)
#     player_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("PlayerPicks.player_id"), index=True
#     )
# 
# Path: nflpool/data/teaminfo.py
# class TeamInfo(SqlAlchemyBase):
#     __tablename__ = "TeamInfo"
#     team_id = sqlalchemy.Column(sqlalchemy.Integer, index=True, primary_key=True)
#     name = sqlalchemy.Column(sqlalchemy.String)
#     city = sqlalchemy.Column(sqlalchemy.String)
#     team_abbr = sqlalchemy.Column(sqlalchemy.String)
#     conference_id = sqlalchemy.Column(sqlalchemy.Integer)
#     division_id = sqlalchemy.Column(sqlalchemy.Integer)


# Path: nflpool/services/nfl_player_rank_service.py
from nflpool.data.dbsession import DbSessionFactory
from nflpool.data.divisioninfo import DivisionInfo
from nflpool.data.conferenceinfo import ConferenceInfo
from nflpool.data.picktypes import PickTypes
from nflpool.data.weekly_nflplayer_stats import WeeklyNFLPlayerStats
from nflpool.data.activeplayers import ActiveNFLPlayers
from nflpool.data.teaminfo import TeamInfo
from itertools import groupby



def interception_leaders():
    session = DbSessionFactory.create_session()

    afc_interception_query = (
        session.query(
            WeeklyNFLPlayerStats.interceptions, WeeklyNFLPlayerStats.player_id
        )
        .join(ActiveNFLPlayers)
        .outerjoin(TeamInfo, ActiveNFLPlayers.team_id == TeamInfo.team_id)
        .filter(TeamInfo.conference_id == 0)

        .filter(WeeklyNFLPlayerStats.interceptions)