# %%
PROJECT_PATH = '/home/md/Work/ligalytics/leagues_stable/'
import os, sys
sys.path.insert(0, PROJECT_PATH)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "leagues.settings")
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

from leagues import settings
# settings.DATABASES['default']['NAME'] = PROJECT_PATH+'/db.sqlite3'
settings.DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
settings.DATABASES['default']['HOST'] = '0.0.0.0'
settings.DATABASES['default']['PORT'] = '5432'
settings.DATABASES['default']['USER'] = 'postgres'
settings.DATABASES['default']['PASSWORD'] = 'secret123'
settings.DATABASES['default']['NAME'] = 'mypgsqldb'
settings.DATABASES['default']['ATOMIC_REQUESTS'] = False
settings.DATABASES['default']['AUTOCOMMIT'] = True
settings.DATABASES['default']['CONN_MAX_AGE'] = 0
settings.DATABASES['default']['CONN_HEALTH_CHECKS'] = False
settings.DATABASES['default']['OPTIONS'] = {}


os.environ["XPRESSDIR"] = "/opt/xpressmp"
os.environ["XPRESS"] = "/opt/xpressmp/bin"
os.environ["LD_LIBRARY_PATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["DYLD_LIBRARY_PATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["SHLIB_PATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["LIBPATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["PYTHONPATH"] = os.environ["XPRESSDIR"] + "/lib"
os.environ["CLASSPATH"] = os.environ["XPRESSDIR"] + "/lib/xprs.jar"
os.environ["CLASSPATH"] = os.environ["XPRESSDIR"] + "/lib/xprb.jar" + os.pathsep + os.environ["CLASSPATH"]
os.environ["CLASSPATH"] = os.environ["XPRESSDIR"] + "/lib/xprm.jar" + os.pathsep + os.environ["CLASSPATH"]
os.environ["PATH"] = os.environ["XPRESSDIR"] + "/bin" + os.pathsep + os.environ["PATH"]

import django
django.setup()

from django.shortcuts import HttpResponseRedirect
from django.http import HttpResponse, JsonResponse
from django.utils import timezone
from django.urls import reverse
from django.core.files.storage import FileSystemStorage
from django.core.mail import send_mail
from django_tex.shortcuts import render_to_pdf

from celery.result import AsyncResult
import googlemaps
import timeit
import random
import json
import builtins as __builtin__
import csv

from leagues.celery import celery
from leagues.settings import EMAIL_DEFAULT_FROM, EMAIL_DEFAULT_TO
from leagues.settings import RUN_ENV, INSTANCE, DEBUG
from common.tasks import log_telegram
from common.functions import *
from scheduler.models import *
from scheduler.helpers import *
from scheduler.widgets import widget_context_kpis
from scheduler.solver.optimizer import optimize_2phases, optimize_sequentially
import scheduler.solver.optimizer as so
from draws.solver import optimize_draws

import time as timer

from qualifiers.helpers import import_globals



# %%

scenario = Scenario.objects.get(id=10312)
teams = scenario.season.scheduler_teams.all()


# %%

from draws.models import SuperGroup,Draw

base_draw = Draw.objects.filter(season=scenario.season).first()

supergroups = SuperGroup.objects.filter(draw=base_draw)   
GameRequirement.objects.filter(scenario=scenario).delete()
for supergroup in supergroups:
    games = optimize_draws.simulate_draws(supergroup.id, 1,report=False)       
    gamereqs = []
    top_games = []
    eng_top = []
    fra_top = []
    ger_top = []            
    ita_top = []
    esp_top = []
    for g in games:
        team1 = teams.get(id=g[0])
        team2 = teams.get(id=g[1])
        attractivity = team1.attractivity*team2.attractivity
        if attractivity >= 20:
            top_games.append((g[0],g[1],attractivity))
        if team1.countryObj.shortname == "ENG" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
            team2.countryObj.shortname == "ENG" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
            eng_top.append((g[0],g[1],attractivity))
        if team1.countryObj.shortname == "FRA" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
            team2.countryObj.shortname == "FRA" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
            fra_top.append((g[0],g[1],attractivity))
        if team1.countryObj.shortname == "GER" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
            team2.countryObj.shortname == "GER" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
            ger_top.append((g[0],g[1],attractivity))
        if team1.countryObj.shortname == "ITA" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
            team2.countryObj.shortname == "ITA" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
            ita_top.append((g[0],g[1],attractivity))
        if team1.countryObj.shortname == "ESP" and team2.countryObj != team1.countryObj and team2.attractivity >= 3 or \
            team2.countryObj.shortname == "ESP" and team1.countryObj != team2.countryObj and team1.attractivity >= 3:
            esp_top.append((g[0],g[1],attractivity))
                
        gamereqs.append(GameRequirement(scenario=scenario, team1=teams.get(id=g[0]), team2=teams.get(id=g[1]), number=1))
    GameRequirement.objects.bulk_create(gamereqs)
    
    encgroups = EncGroup.objects.filter(scenario=scenario)
    Encounter.objects.filter(scenario=scenario).delete()

    for g in sorted(top_games, key=lambda x: x[2], reverse=True):
        enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="Top Games"))
        enc.save()
        enc.homeTeams.add(g[0])
        enc.awayTeams.add(g[1])
    eng_top = sorted(eng_top, key=lambda x: x[2], reverse=True)[:8]
    for g in eng_top:
        enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="England top games"))
        enc.save()
        enc.homeTeams.add(g[0])
        enc.awayTeams.add(g[1])
    fra_top = sorted(fra_top, key=lambda x: x[2], reverse=True)[:8]
    for g in fra_top:
        enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="France top games"))
        enc.save()
        enc.homeTeams.add(g[0])
        enc.awayTeams.add(g[1])
    ger_top = sorted(ger_top, key=lambda x: x[2], reverse=True)[:8]
    for g in ger_top:
        enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="German top games"))
        enc.save()
        enc.homeTeams.add(g[0])
        enc.awayTeams.add(g[1])
    ita_top = sorted(ita_top, key=lambda x: x[2], reverse=True)[:8]
    for g in ita_top:
        enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="Italian top games"))
        enc.save()
        enc.homeTeams.add(g[0])
        enc.awayTeams.add(g[1])
    esp_top = sorted(esp_top, key=lambda x: x[2], reverse=True)[:8]
    for g in esp_top:
        enc = Encounter(scenario=scenario,encounterGroup=encgroups.get(name="Spain top games"))
        enc.save()
        enc.homeTeams.add(g[0])
        enc.awayTeams.add(g[1])

    # adjust distribution of topgames
    EncWish.objects.filter(scenario=scenario,reason="ITA top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(ita_top)*0.4),maxGames=int(len(ita_top)*0.6+0.5))
    EncWish.objects.filter(scenario=scenario,reason="ENG top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(eng_top)*0.4),maxGames=int(len(eng_top)*0.6+0.5))
    EncWish.objects.filter(scenario=scenario,reason="FRA top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(fra_top)*0.4),maxGames=int(len(fra_top)*0.6+0.5))
    EncWish.objects.filter(scenario=scenario,reason="ESP top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(esp_top)*0.4),maxGames=int(len(esp_top)*0.6+0.5))
    EncWish.objects.filter(scenario=scenario,reason="GER top games should not be scheduled more than 60% of the time on the same weekday").update(minGames=int(len(ger_top)*0.4),maxGames=int(len(ger_top)*0.6+0.5))


exit()


# %%
t_dict = {
    "Getafe C.F.": 87960,
    "FC Shkupi": 2606993,
    "Maccabi Tel-Aviv FC": 57477,
    "Malatyaspor": 78058,
    "RCD Espanyol de Barcelona": 54189,
    "Tottenham Hotspur": 1652,
    "Trabzonspor A.Ş.": 52731,
    "AS Monaco": 50023,
    "Fotbal Club FCSB":  50065,
    "FK Radnički Niš": 52734,
    "F91 Dudelange": 59028,
    "Progrès Niederkorn": 52311,
    "IFK Norrköping":  50099,
    "FC Levadia Tallinn": 77482,
    "LASK Linz": 63405,
    "GKS Piast Gliwice":  2600545,
    "MKS Cracovia Kraków":  88133,
    "Sabail": 2608569,
    "RC Strasbourg": 59857,
    "FK Makedonija Skopje": 64395,
    "PFC Arsenal Tula": 2606414,
    "U Craiova 1948 Club Sportiv": 64503,
    "VfL Borussia Mönchengladbach":  52757,
    "PFC Lokomotiv Plovdiv 1926": 57466,
    "Football Club Zenit": 52826,
    "Lechia Gdańsk": 52763,
    "Hapoel Beer-Sheva FC": 59340, 
    "SP La Fiorita": 64508,
    "KF Feronikeli": 2608281,
    "Cardiff MUFC": 59337,
    "Barry Town UFC": 53069,
    "Beşiktaş": 50157,
    "Mons Calpe Sports Club": 2608011,
    "FC St.Gallen 1879": 51151,
    "FC Zimbru Chişinău": 59036,
    "Hapoel Be'er Sheva FC": 59340,
    "PFC Arda Kardzhali 1924": 2610060,
    "FC Torpedo-Belaz Zhodino": 79551,
    "Panevezys": 2608927,
    "Ballkani": 2609552,
    "Dnipro-1": 2609193,
    "Union Saint-Gilloise": 64125,
    "Omonoia FC": 50077,
    "R. Charleroi SC": 52886,
    "Manchester City FC": 52919,
    "Manchester United FC": 52682,
    "Barry Town United FC": 53069,
    "FC CSKA 1948": 2610262,
    "Sabah": 2609356,
    "FC Bologna": 52969,
    "FC Dynamo Brest": 64374,
    "Kisvarda Master Good": 2605958,
    "FC Bologna": 52969,
    "Beşiktaş": 50157,
    "Omonoia FC": 50077,
    "WKS Śląsk Wrocław": 52655,
    "FC Ararat Yerevan": 59318,
    "FCB Magpies": 2606279,
    "KKS Lech Poznań": 64227,
    "Sumgait FC": 2603921,
    "KF Dukagjini": 2608311,
    "FC Haka Valkeakoski": 52802,
    "FC Hegelmann Litauen": 2607108,
    "FCV Farul": 2604753,
    "FC Petrocub Hîncești": 2607112,
    "Kolos Kovalivka": 2609189,
    "St. Patrick Athletic FC": 50133,
    "Manchester United FC": 52682,
    "Trabzonspor AS": 52731,
    "İstanbul Basaksehir FK": 2600288,
    "Borussia VfL 1900 Mönchengladbach": 52757,
}



for t in teams:
    
    gt = t.global_id
    if not gt:
        gt = GlobalTeam.objects.filter(name=t.name).first()
        
    if not gt:
        gt = GlobalTeam.objects.filter(fame_id=t_dict.get(t.name)).first()
        
    if not gt:
        print(t.name)


    t.name = gt.name
    t.global_id = gt
    t.external_id = gt.fame_id
    t.save()

        
    
    




# %%


# %%

new_teams = []
teams.update(active=False)
with open('calendar_juneteams/ucl_teams.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    next(reader, None)
    for row in reader:
        team_name = row[1].split("(")[0].strip()
        if row[0] != "":
            # print(row)
            team_pos = int(row[0])
            team_name = row[1].split("(")[0].strip()
            team_coeff = row[2]
            
            t = teams.filter(name=team_name).first()
            gt = GlobalTeam.objects.filter(name=team_name).first()
            
            if not t:
                print(f"Team {team_name} not found")
                
                
                if not gt:
                    print(f"Global team {team_name} not found")
                else:
                    t = Team.objects.create(
                        season=scenario.season,
                        name=team_name,
                        shortname=gt.shortname,
                        position=team_pos,
                        latitude=gt.latitude,
                        longitude=gt.longitude,
                        countryObj=Country.objects.get(season=scenario.season,shortname=gt.country.uefa),
                        active=True
                    )
                    
            if not t:
                print("Could not create team", team_name)
                continue
            
            t.global_id = gt
            t.external_id = gt.fame_id
            t.active=True
            t.position = team_pos
            
            if team_pos <= 9:
                t.pot = 1
            elif team_pos <= 18:
                t.pot = 2
            elif team_pos <= 27:
                t.pot = 3
            else:
                t.pot = 4
            
            t.save()
                
                
                
            
            


# %%




CET_minus_1 = ['ENG','POR','SCO','ISL','FRO']
CET_plus_1 = ['TUR','GRE','ROU','LTU','FIN','EST','MDA','CYP','AZE','ISR','UKR']


for i in range(1,5):
    conf = Conference.objects.get_or_create(scenario=scenario,name=f"UCL-Pot {i}")
    conf[0].teams.clear()
    

for i in range(1,6):
    conf = Conference.objects.get_or_create(scenario=scenario,name=f"Global Coeff {i}")
    conf[0].teams.clear()
    conf = Conference.objects.get_or_create(scenario=scenario,name=f"Domestic Coeff {i}")
    conf[0].teams.clear()
conf = Conference.objects.get_or_create(scenario=scenario,name="CET")
conf[0].teams.clear()
conf = Conference.objects.get_or_create(scenario=scenario,name="CET-1")
conf[0].teams.clear()
conf = Conference.objects.get_or_create(scenario=scenario,name="CET+1")
conf[0].teams.clear()

# for conf in Conference.objects.filter(scenario=scenario).exclude(name__in=['HARD Constraints','SOFT Constraints']):
#     conf[0].teams.clear()
#     conf.collapseInView = True
#     conf.save()



# Team.objects.filter(season=scenario.season).update(active=False)
# for t in new_teams:   

#     team_name = t[1].split('(')[0].strip()
#     team_country = t[1].split('(')[1].split(')')[0].strip()
#     # abbreviation = t[2]
#     global_coeff = t[4]
#     domestic_coeff = t[5]
#     pot = int(t[3].split(' ')[1].strip())
#     pos = int(t[0])
#     competition = "UCL"
#     teamObj = Team.objects.filter(season=scenario.season,name=team_name)
#     if teamObj:        
        
#         pass
#     else:
#         print(t,"->", team_name)
#         gteam = GlobalTeam.objects.filter(name=team_name)
#         if gteam:
#             teamObj = Team.objects.create(season=scenario.season,
#                 name=team_name,
#                 attractivity=global_coeff+0.1*domestic_coeff,
#                 position=pos,
#                 pot=pot,
#                 latitude=gteam.first().latitude,
#                 longitude=gteam.first().longitude,
#                 country=gteam.first().country,
#                 active=True)
#             print("\tCreated team from global", team_name)
#             teamObj = Team.objects.filter(season=scenario.season,name=team_name)
#         else:
#             print("\tTeam not found", team_name)
#             continue



for team in Team.objects.filter(season=scenario.season,active=True):
    
    teamObj = Team.objects.filter(id=team.id)
    competition = "UCL"
    global_coeff = int(team.attractivity)
    domestic_coeff = int(team.attractivity*10)%10
    team_country = team.countryObj.shortname
    
    Conference.objects.filter(scenario=scenario,name=competition).first().teams.add(teamObj.first())
    Conference.objects.filter(scenario=scenario,name=f"{competition}-Pot {team.pot}").first().teams.add(teamObj.first())

    if global_coeff in range(1,6):
        Conference.objects.filter(scenario=scenario,name=f"Global Coeff {global_coeff}").first().teams.add(teamObj.first())

    if domestic_coeff in range(1,6):
        Conference.objects.filter(scenario=scenario,name=f"Domestic Coeff {domestic_coeff}").first().teams.add(teamObj.first())

    if team_country in CET_minus_1:
        Conference.objects.filter(scenario=scenario,name="CET-1").first().teams.add(teamObj.first())
    elif team_country in CET_plus_1:
        Conference.objects.filter(scenario=scenario,name="CET+1").first().teams.add(teamObj.first())
    else:
        Conference.objects.filter(scenario=scenario,name="CET").first().teams.add(teamObj.first())

    teamObj.update(coefficient=5-team.pot)


for conf in Conference.objects.filter(scenario=scenario):
    for t in conf.teams.filter(active=False):
        conf.teams.remove(t)

for haw in HAWish.objects.filter(scenario=scenario):
    for t in haw.teams.filter(active=False):
        haw.teams.remove(t)

for enc in EncWish.objects.filter(scenario=scenario):
    for t in enc.teams1.filter(active=False):
        enc.teams1.remove(t)
    for t in enc.teams2.filter(active=False):
        enc.teams1.remove(t)

for pair in Pairing.objects.filter(scenario=scenario):
    if pair.team1.active==False or pair.team2.active==False:
        pair.active=False
        pair.save()



# %%

# %%
