from python_graphql_client import GraphqlClient  
import os
import sys

GRAPHQL_URI = os.environ.get('DOCKER_GRAPHQL_URI', 'https://staging.gepick.com/graphql')

if len(sys.argv) > 2:
    GRAPHQL_URI = sys.argv[2]

print('GRAPHQL_URI:'+GRAPHQL_URI)

client = GraphqlClient(endpoint=GRAPHQL_URI)


queryMatchesByLeagueId = """
    query matches($args: MatchesQueryInput!) {
        matches(args:$args) {
            _id
            goalsHomeTeam
            goalsAwayTeam
            status
        }
    }
"""

queryMatchesByIds = """
query Matches($matchIds: [String!]!, $historicalDeep: Float!) {
    matches: matchesByIds(ids: $matchIds, historicalDeep: $historicalDeep) {
      _id
      startTime
      homeTeam {
        _id
      }
      awayTeam {
        _id
      }
      goalsHomeTeam
      goalsAwayTeam
      league{
        _id    
      }
      status
      historicalMatches {
        home {
          _id
          homeTeam {
            _id
          }
          awayTeam {
            _id
          }
          goalsHomeTeam
          goalsAwayTeam
          startTime
          status
        }
        away {
          _id
          homeTeam {
            _id
          }
          awayTeam {
            _id
          }
          goalsHomeTeam
          goalsAwayTeam
          startTime
          status
        }
      }
    }
  }
"""

def getMatchesByLeagueId(leagueId):
    variables = { "args" : {"leagueId": leagueId}}
    data = client.execute(query=queryMatchesByLeagueId, variables=variables)
    return data["data"]["matches"]

def getMatchesByIds(matchIds):
    variables = {"matchIds": matchIds, "historicalDeep": 10}  
    apiData = client.execute(query=queryMatchesByIds, variables=variables)
    
    matches = apiData["data"]["matches"]
    
    return matches
    