import pandas as pd


def get_rahmentermine(MS_ART, nTeams):
  
    """ LOAD RAHMENTERMINPLAN FROM CSV"""
    rahmentermine = pd.read_csv("data/rahmentermine.csv")
    cols = list(rahmentermine.columns)

    # %%

    rahmentermine['From'] = pd.to_datetime(rahmentermine['From'], format='%d/%m').dt.date
    rahmentermine['From'] = rahmentermine['From'].apply(lambda x: x.replace(year=2025) if x.month < 7 else x.replace(year=2024))
    rahmentermine['To'] = pd.to_datetime(rahmentermine['To'], format='%d/%m').dt.date
    rahmentermine['To'] = rahmentermine['To'].apply(lambda x: x.replace(year=2025) if x.month < 7 else x.replace(year=2024))

    # %%

    """ REMOVE ALL ENTRIES THAT ARE NO REAL MATCHDAYS"""
    for col in cols:
        if col in ['From', 'To']:
            continue
        rahmentermine[col] = pd.to_numeric(rahmentermine[col], errors='coerce')
        rahmentermine[col] = rahmentermine[col].apply(lambda x: int(x) if pd.notna(x) and x == int(x) else '')

    # %%

    """ CREATE DICTIONARY WITH MATCHDAYS"""
    termine = {}
    for col in cols:
        colname = col.split('.')[0]
        if col in ['From', 'To']:
            continue
        if rahmentermine[col][0] != nTeams:
            continue
        termine[(colname,nTeams)] = {}
        
        for row in rahmentermine.iterrows():
            if row[0] == 0:
                continue
            if row[1][col] != "":
                termine[(colname,nTeams)][row[1][col]] = [row[1]['From'], row[1]['To']]

    return termine.get((MS_ART,nTeams), [])

        
        
        