from datetime import date
from typing import List

from sqlmodel import select

from config.config_utils import get_managed_teams_config
from src.api.fixtures_client import FixturesClient
from src.db.db_manager import NotifierDBManager
from src.db.notif_sql_models import Fixture as DBFixture
from src.db.notif_sql_models import League as DBLeague
from src.db.notif_sql_models import Team as DBTeam
from src.entities import Championship, Team
from src.utils.fixtures_utils import convert_fixture_response_to_db

NOTIFIER_DB_MANAGER = NotifierDBManager()


def insert_league(fixture_league: Championship) -> DBLeague:
    league_statement = select(DBLeague).where(DBLeague.id == fixture_league.league_id)
    retrieved_league = NOTIFIER_DB_MANAGER.select_records(league_statement)

    if not len(retrieved_league):
        league = DBLeague(
            id=fixture_league.league_id,
            name=fixture_league.name,
            logo=fixture_league.logo,
            country=fixture_league.country,
        )
        NOTIFIER_DB_MANAGER.insert_record(league)
        retrieved_league = NOTIFIER_DB_MANAGER.select_records(league_statement)

    return retrieved_league


def insert_team(fixture_team: Team) -> DBTeam:
    team_statement = select(DBTeam).where(DBTeam.id == fixture_team.id)
    retrieved_team = NOTIFIER_DB_MANAGER.select_records(team_statement)

    if not len(retrieved_team):
        team = DBTeam(
            id=fixture_team.id,
            name=fixture_team.name,
            picture=fixture_team.picture,
            aliases=fixture_team.aliases,
        )
        NOTIFIER_DB_MANAGER.insert_record(team)
        retrieved_team = NOTIFIER_DB_MANAGER.select_records(team_statement)

    return retrieved_team


def save_fixtures(team_fixtures: List[dict]) -> None:
    converted_fixtures = []
    fix_nr = 1
    for fixture in team_fixtures:
        print(f"Converting & populating fixture {fix_nr}/{len(team_fixtures)}")
        converted_fixtures.append(convert_fixture_response_to_db(fixture))
        fix_nr += 1

    db_fixtures = []

    for conv_fix in converted_fixtures:
        retrieved_league = insert_league(conv_fix.championship)
        retrieved_home_team = insert_team(conv_fix.home_team)
        retrieved_away_team = insert_team(conv_fix.away_team)

        fixture_statement = 