# %%
PROJECT_PATH = '/home/md/Work/ligalytics/leagues_develop/'
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'

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 qualifiers.models import *
from common.models import GlobalTeam, GlobalCountry
from scheduler.models import Season, Scenario, Team, Day, CountryClash, Country

from qualifiers.draws import groupTeams, optimize_inversions4
from scheduler.solver.tasks.optimize import optimize
from common.functions import distanceInMilesByGPS

from referees.helpers import use_referees, import_deb_delegates, seed_rounds_from_days, generate_distance_matrix

from referees.models import *

import random
import time
import json
import csv
import networkx as nx
import matplotlib.pyplot as plt
from dateutil import parser
from datetime import timedelta
import googlemaps
import pandas as pd
# %%

scenario = Scenario.objects.get(id=4)
season= scenario.season

# %%
# from referees.optimize import optimize_referees, optimize_referees_reopt, optimize_delegates, optimize_wales
# sol=optimize_wales(scenario.id, "md", True, RUN_ENV='NO_CELERY')

# %%

tt = time.time()
for d in Delegate.objects.filter(season=season).prefetch_related('assignments'):
# for d in Delegate.objects.filter(season=season).prefetch_related('assignments').only('id'):
# for d in Delegate.objects.filter(season=season):
    for a in d.assignments.all():
        x = a.travel
    
    
print(time.time()-tt)


# %%

# categories = Category.objects.filter(season=season,requirements__in=Requirement.objects.filter(season=season))
# delegates = Delegate.objects.filter(season=season,active=True,category__in=categories)

# Delegate.objects.filter(season=season).exclude(id__in=delegates.values('id')).update(active=False)

# for d in Delegate.objects.filter(season=season):
#     if d.location:
#         d.latitude = d.location.latitude
#         d.longitude = d.location.longitude    
#         d.save()

# %%

# for d in Delegate.objects.filter(season=season):
#     d.name = f"Person {d.name}"
#     d.save()
# # %%
# competitions = Competition.objects.filter(season=season)
# days = Day.objects.filter(season=season).exclude(round=7)
# # %%


# for d in Delegate.objects.filter(season=season):
#     d.coefficient = d.category.order
#     d.save()


# from referees.optimize import optimize_referees, optimize_referees_reopt, optimize_delegates, optimize_wales
# sol=optimize_wales(2, "md", True, RUN_ENV='NO_CELERY')
# # %%

# ranks = [
# 'Elite Group One',
# 'Elite Group Two',
# '2',
# '3',
# '4A',
# '4B',
# '4C',
# '4D',
# 'AJR',
# 'CJR',
# 'International AR (FIFA List)',
# '1AR',
# '1ART',
# '2AR',
# '3AR',
# ]

# for i,r in enumerate(ranks):
#     Category.objects.filter(name=r).update(order=i+1)

# # %%


# for d in Delegate.objects.filter(season=season):
#     group,_ = DelGroup.objects.get_or_create(season=season, name=d.category)
#     group.delegates.add(d)



# %%
