#!/usr/bin/env python3
"""Script to write out all statistics to analyze for a given roster and league.

Usage:
    env PREFECT__FLOWS__CHECKPOINTING=true write_all_statistics \
        --league_config <path to league config JSON file> \
        --output_directory <output directory for data>
"""
import argparse
import os
import prefect
from prefect import Flow, Parameter

from fantasy.tasks import (
    League,
    get_league_deviation_statistics,
    get_league_mean_statistics,
    get_normalized_player_statistics,
    get_normalized_roster_statistics,
    get_player_deviation_statistics,
    get_player_mean_statistics,
    get_teams,
    get_team_rosters,
    parse_player_statistics,
    parse_roster_statistics,
    compute_team_roster_relevances,
    compute_trade_relevances,
)


with Flow(name='Write All Statistics') as flow:
    season = Parameter(name='season', default=2021)
    teams = get_teams(season=season)
    player_info = parse_player_statistics(season=season)
    player_stats_mean, player_stats_deviation = (
        get_player_mean_statistics(player_info),
        get_player_deviation_statistics(player_info),
    )
    rosters = get_team_rosters(season=season, teams=teams)
    roster_stats = parse_roster_statistics(season=season, rosters=rosters, player_info=player_info)
    league_stats_mean, league_stats_deviation = (
        get_league_mean_statistics(roster_stats),
        get_league_deviation_statistics(roster_stats),
    )
    normalized_player_statistics = get_normalized_player_statistics(
        teams=teams,
        player_statistics=player_info,
        player_mean_statistics=player_stats_mean,
        player_deviation_statistics=player_stats_deviation,
    )
    normalized_roster_stats = get_normalized_roster_statistics(
        teams=teams,
        roster_statistics=roster_stats,
        league_mean_statistics=league_stats_mean,
        league_deviation_statistics=league_stats_deviation,
    )
    team_roster_relevances = compute_team_roster_relevances(
        team_rosters=rosters,
        player_stats=normalized_player_statistics,
        roster_stats=normalized_roster_stats,
    )

    trade_relevances = compute_trade_relevances(
        team_rosters=rosters,
        player_stats=normalized_player_statistics,
        roster_stats=normalized_roster_stats,
    )

if __name__ == '__main__':
    parser = argparse.ArgumentParser('Write all fantasy statistics to file for analysis.')
    parser.add_argument(
        '--league_config',
        type=str,
        default='./league_config.json',
        help="Config (cookies and similar) for the league to analyze.",
    )
    parser.add_argument(
        '--output_directory',
        type=str,
        default='./data/',
        help="Directory to write all analysis data to.",
    )
    args = parser.parse_args()
    absolute_output_directory = os.path.abspath(args.output_directory)
    print(f'Writing results to {absolute_output_directory}.')
    prefect.config.flows.checkpointing = True
    with prefect.context(
        league=League.load_league(args.league_config), output_directory=absolute_output_directory
    ):
        final_state = flow.run()
