# %%
import requests
import json

prod_url = 'https://uefadigitalapi.developer.azure-api.net'
prod_primary_key = '7dfa861240aa40f8a834990c24f1a66d'
prod_secondary_key = '4451dcc1ad4f41b2aa6af96cc5a1256a'
pre_url = 'https://uefadigitalapipre.developer.azure-api.net'
pre_primary_key = '1decf93425944f8b9e6dc7226a3b8477'
pre_secondary_key = '14771f5c67b74836a59f777cb543cc0f'


""" GET ALL COMPETITIONS """
# %%
r=requests.get("https://api.digital.uefa.com/comp/v2/competitions", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})
result = r.json()
for r in result:
    print(r['id'],r['metaData']['name'])


# %%
""" All seasons for a single competition """

# competitionId = 1 # Champions League
# competitionId = 14 # Europa League
competitionId = 2014

r=requests.get(f"https://api.digital.uefa.com/comp/v2/competitions/{competitionId}/seasons", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})
result = r.json()
# for r in result:
#     print(r['id'],r['name'])
result


# %%
""" Single Season """

competitionId = 1 # Champions League
seasonYear = 2025

r=requests.get(f"https://api.digital.uefa.com/comp/v2/competitions/{competitionId}/seasons/{seasonYear}", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})
r.json()

# %%
""" Competition Structure by filter """
competitionId = 1 # Champions League
seasonYears = 2025

r=requests.get(f"https://api.digital.uefa.com/comp/v2/competition-structure?competitionId={competitionId}&seasonYears={seasonYears}", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})
r.json()


# %%

""" Team by filter or ids """
# https://api.digital.uefa.com/comp/v2/teams[?teamIds][&roundIds][&competitionId][&seasonYear][&phase][&associationId][&teamType][&offset][&limit]
competitionId = 1 # Champions League
# competitionId = 14 # Europa League
# competitionId = 2014 # Europa League
seasonYear = 2025

r=requests.get(f"https://api.digital.uefa.com/comp/v2/teams?competitionId={competitionId}&seasonYear={seasonYear}&phase=ALL&offset=1&limit=200", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})
result = r.json()

# write json file
json.dump(result, open('uefa_teams.json','w'))
# print(result)

# for r in result:
#     print(r['id'],r['translations']['displayOfficialName']['EN'],r['teamCode'])
    # print("\t",r)


# %%

""" Matchdays by filter """
# https://api.digital.uefa.com/comp/v2/matchdays[?competitionId][&seasonYear][&matchdayIds]
competitionId = 1 # Champions League
seasonYear = 2025

r=requests.get(f"https://api.digital.uefa.com/comp/v2/matchdays?competitionId={competitionId}&seasonYear={seasonYear}", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})
result = r.json()

result

# %%
""" Latest coefficient """
seasonYear = 2024
coeff_res  = requests.get(url=f"https://api.digital.uefa.com/comp/v2/coefficients?coefficientType=MEN_CLUB&coefficientRange=OVERALL&seasonYear={seasonYear}&pagesize=500", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})

for coeff_json in coeff_res.json()['data']['members']:
    print(coeff_json['member']['displayOfficialName'], coeff_json['overallRanking'])



# %%%


# %%

# %%

r=requests.get("https://api.digital.uefa.com/comp/v2/competitions/1/seasons", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})
# %%
r.json()
# %%

# r=requests.get("https://api.pre.digital.uefa.com/comp/v2/competitions/1/seasons", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"14771f5c67b74836a59f777cb543cc0f"})
# # %%
# r.json()
# # %%



# TEAM by filter or ids
# https://api.digital.uefa.com/comp/v2/teams[?teamIds][&roundIds][&competitionId][&seasonYear][&phase][&associationId][&teamType][&offset][&limit]


r=requests.get("https://api.digital.uefa.com/comp/v2/teams?teamIds=50037", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})

# %%
r.json()
# %%


r=requests.get("https://api.digital.uefa.com/comp/v2/stadiums?limit=1&offset=1", headers={"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"})


# %%

r.json()
# %%

list_teams = [52919,50080,52973,50031,50123,52749,59324,77977,52277,52682,50138,50069,50136,50050,50058,50062,52709,52816,52707,7889,52714,52280,52748,50067,50124,60609,50113,50147,2603790,52336,50037,50030,50064,52758,52747,50051]

get_teams = "52919,50080,52973,50031,50123,52749,59324,77977,52277,52682,50138,50069,50136,50050,50058,50062,52709,52816,52707,7889,52714,52280,52748,50067,50124,60609,50113,50147,2603790,52336,50037,50030,50064,52758,52747,50051"


uefa_coefficients_url = 'https://api.digital.uefa.com/comp/v2/coefficients?coefficientType=MEN_CLUB&coefficientRange=SEASONAL&seasonYear=2024&memberId='

uefa_teams_url = 'https://api.digital.uefa.com/comp/v2/teams?teamIds='+get_teams
headers = {"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"}
# %%

res = requests.get(uefa_teams_url, headers=headers)

# %%
res.json()
# %%

for team in res.json():
    print(team['id'],team['translations']['displayOfficialName']['EN'],team['teamCode'])
    coefficient = requests.get(uefa_coefficients_url+str(team['id']), headers=headers)
    print(coefficient.json()['data']['members'][0]['seasonRankings'])
    
    
    

# %%

# res.json()
# # %%


# for r in res.json()['data']['members']:
#     print(r['member']['id'],r['overallRanking']['position'],)
# # %%

uefa_match_url = "https://api.digital.uefa.com/match/v5/matches?offset=0&limit=10&order=DESC&competitionId=1&seasonYear=2024&seasonsRange=ALL&phase=ALL"
headers = {"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"}
# %%

res = requests.get(uefa_match_url, headers=headers)
# %%
res.json()
# # %%


### TEAMS

# GET https://api.digital.uefa.com/comp/v2/teams?competitionId=1&seasonYear=2024&phase=ALL&offset=1&limit=500 HTTP/1.1
# %%


""" POSITIONS """
 
uefa_api_prefix = 'https://api.digital.uefa.com/comp/v2' 
seasonYear = 2025
uefa_coefficients_url = f'{uefa_api_prefix}/coefficients?coefficientType=MEN_CLUB&coefficientRange=OVERALL&seasonYear={seasonYear}&pagesize=500'
headers = {"Cache-Control":"no-cache","Ocp-Apim-Subscription-Key":"7dfa861240aa40f8a834990c24f1a66d"}

coeff_res = requests.get(uefa_coefficients_url, headers=headers)
changed= []
for coeff_json in coeff_res.json()['data']['members']:      
    print(coeff_json['member']['id'], coeff_json['overallRanking']['totalValue'])
# %%
