# %%
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 scheduler.models import *
import pulp
from pulp import lpSum, value, XPRESS, GUROBI, PULP_CBC_CMD
from django.db.models import Q
from django.template.loader import render_to_string

from scheduler.models import *


import random
import time
import json
import csv
import networkx as nx
import matplotlib.pyplot as plt
from datetime import timedelta

from django.contrib.sessions.models import Session
from pprint import pprint

ucl_basescenario = Scenario.objects.get(id=9543)
# scenario = Scenario.objects.get(id=9368)
# scenario = Scenario.objects.get(id=1)
season = ucl_basescenario.season


days = Day.objects.filter(season=season)
teams = Team.objects.filter(season=season)

getTeamById = {team.id:team for team in teams}
getDayById = {day.id:day for day in days}

user_name = 'md'
user_is_staff = True
runMode = 'New'
localsearch_time = 0
RUN_ENV = 'local'
SOLVER = 'xpress'


solverstats = json.loads(ucl_basescenario.solverstats_json)

solverstats.keys()

# home_in_round = {
#     (r,t):0 for t in teams for r in range(1,season.nRounds+1)
# }


# away_in_round = {
#     (r,t):0 for t in teams for r in range(1,season.nRounds+1)
# }

# scenarios = season.scenarios.exclude(id=ucl_basescenario.id)

# for scenario in scenarios:
    
#     solverstats = json.loads(scenario.solverstats_json)
    
#     if solverstats.get('info-Improve'):
#         print(solverstats['info-Improve'])
#         break
        
    
#     # solution = scenario.sol_solution()
    
#     for game in solverstats['premodell']['partial_solution']:
#         home_in_round[(getDayById[game['days'][0]].round,getTeamById[game['home'][0]])] += 1
#         away_in_round[(getDayById[game['days'][0]].round,getTeamById[game['away'][0]])] += 1
# # sol = optimize(task=None, s2=ucl_basescenario.id, user_name=user_name, user_is_staff=user_is_staff,
# #                 runMode=runMode, localsearch_time=localsearch_time, RUN_ENV=RUN_ENV, solver=SOLVER)
# home_in_round = sorted(home_in_round.items(), key=lambda x: x[0][0])
# pprint(home_in_round)
# away_in_round = sorted(away_in_round.items(), key=lambda x: x[0][0])
# pprint(away_in_round)

# %%
