# %%

# Wichtiger Header + Xpress



"""
##############################################################################################################
"""

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()


import csv

"""
##############################################################################################################
"""

# %%



from scheduler.models import *
from referees.models import *

scenario = Scenario.objects.get(id=10991)
season= scenario.season
# Day.objects.filter(season=scenario.season).delete()


# %%
from referees.optimize import optimize_wales
optimize_wales(scenario.id, "user_name", True)

# %%
# %%
# %%
# %%
# %%
# %%

# with open('referees/fixtures/metalligen2425/2425_arenas.csv','r') as f:
#     reader = csv.reader(f)
#     next(reader)
#     for row in reader:
#         print(row)
#         latitude,longitude = row[3].replace(',','.').split(' ')
#         print(latitude,longitude)
#         Location.objects.get_or_create(season=scenario.season, name=row[1], city=row[4], latitude=latitude, longitude=longitude)
# # %%

# Role.objects.get_or_create(season=scenario.season,name='Referee',order=0,min_required=2,max_required=2)
# Role.objects.get_or_create(season=scenario.season,name='Linesperson',order=1,min_required=2,max_required=2)

# Category.objects.get_or_create(season=scenario.season,name='A',order=0)
# Category.objects.get_or_create(season=scenario.season,name='B',order=1)
# Category.objects.get_or_create(season=scenario.season,name='C',order=2)

# # %%

# import googlemaps
# gmaps = googlemaps.Client(
#     key='AIzaSyB76EhR4OqjdXHQUiTkHZC0Svx_7cPGqyU')

# with open('referees/fixtures/metalligen2425/2425_officials.csv','r') as f:
#     reader = csv.reader(f)
#     next(reader)
#     for row in reader:
#         print(row)
#         id = row[0]
#         name = row[1]
#         zip_code = row[2]
#         city = row[3]
#         region = row[4]
#         role= row[5]
#         category = row[6]
#         target_games = row[7]
        
#         geocode_result = gmaps.geocode(city+" "+zip_code)
#         if len(geocode_result) > 0:
#             location = geocode_result[0]['geometry']['location']
#             print("\t", location)
#         else:
#             location = {'lat': 0, 'lng': 0}
#             print("\t", "NOT FOUND")
        
        
#         delegate, created = Delegate.objects.get_or_create(season=scenario.season,
#             category= Category.objects.filter(season=scenario.season,name=category).first(),
#             name= name,
#             latitude= location['lat'],
#             longitude= location['lng'],
#         )
        
#         location, created = Location.objects.get_or_create(season=scenario.season,
#             name=city,
#             city=city,
#             type=3,
#             latitude=location['lat'],
#             longitude=location['lng'])
        
#         delegate.location = location
#         delegate.save()
        
        
        
        
        
        
# Delegate.objects.filter(season=scenario.season).update(country=Country.objects.get(season=scenario.season,name='Denmark'))
        
        




# %%

# competition, created = Competition.objects.get_or_create(season=season,name='Metalligen 2425')

# # %%

# RefGame.objects.filter(scenario=scenario).delete()
# with open('referees/fixtures/metalligen2425/2425_schedule.csv','r') as f:
#     reader = csv.reader(f)
#     next(reader)
#     for row in reader:
#         # print(row)
        
#         day = datetime.datetime.strptime(row[2],'%m/%d/%Y')
#         date, created = Day.objects.get_or_create(
#             season=season,
#             date=day,
#             day=day.strftime("%d-%m-%Y")
#         )
        
#         time = row[4]
#         timeslot, created = TimeSlot.objects.get_or_create(
#             season=season,
#             name=time,
#             start=time,
#         )
        
        
#         gameID = row[6]
        
#         homeTeam = Team.objects.filter(season=season,name=row[7]).first()
#         awayTeam = Team.objects.filter(season=season,name=row[8]).first()
        
#         if not homeTeam and awayTeam:
#             print("ERROR: ", row)
#             continue
        
#         arena = Location.objects.filter(season=season,name=row[9]).first()
       
#         RefGame.objects.get_or_create(
#             homeTeam=homeTeam,
#             awayTeam=awayTeam,
#             scenario=scenario,
#             timeslot=timeslot,
#             day=date,
#             location=arena,
#             competition=competition,
#         )
        
        
        

# # %%
# Day.objects.filter(season=season).update(round=1)

# %%
# teams = Team.objects.filter(season=season)
# firstday = Day.objects.filter(season=season).order_by('date').first()
# lastday = Day.objects.filter(season=season).order_by('date').last()

# print(firstday.date, lastday.date)

# # %%
# RefWish.objects.filter(scenario=scenario).delete()  
# with open('referees/fixtures/metalligen2425/2425_officials.csv','r') as f:
#     reader = csv.reader(f)
#     next(reader)
#     for row in reader:
#         print(row)
#         id = row[0]
#         name = row[1]
#         zip_code = row[2]
#         city = row[3]
#         region = row[4]
#         role= row[5]
#         category = row[6]
#         target_games = row[7]
        
#         delegate = Delegate.objects.filter(season=season,name=name).first()
        
#         wish = RefWish.objects.create(
#             scenario=scenario,
#             startFirstDay=True,
#             endLastDay=True,
#             day=firstday,
#             day2=lastday,
#             minGames=target_games,
#             maxGames=target_games,
#             reason=f'Target {target_games} Games for {delegate.name} ({delegate.category.name})',
#             prio='A'
#         )
        
        
#         wish.teams.add(*teams)
#         wish.delegates.add(delegate)
        
# # # %%
# for d in Day.objects.filter(season=season):
#     d.day = d.date.strftime("%Y-%m-%d")
#     d.save()
# #             date=day,
# #             day=day.strftime("%d-%m-%Y")
# # %%

# for r in Requirement.objects.filter(season=season):
#     print(r.competition,r.role,r.classification)
# # %%


# game = RefGame.objects.filter(scenario=scenario).first()

# # %%
# for a in Assignment.objects.filter(game=game):
#     print(a.game,a.role,a.delegate)
# # %%
# Delegate.objects.filter(season=season,roles__isnull=True).delete()
# # %%

# %%
