private async Task <List <PlayerStatsView> > SetupPlayerStatsViews(IEnumerable <Guid> statIds, List <ScheduleView> schedule, Dictionary <StatsKey, List <MatchDetailEntity> > matchDetails)
        {
            var playerStats = await _playerStatsRepository.GetStatsAsync(statIds);

            var mappedStats = new List <PlayerStatsView>();

            var scheduleIds = schedule.Select(x => x.ScheduleId).ToList();
            var mvpStats    = await _matchMvpRepository.ReadAllForTeamScheduleIds(scheduleIds);

            foreach (var playerStat in playerStats)
            {
                var playerId   = playerStat.Key.SummonerId;
                var playerMvps =
                    (mvpStats.SelectMany(x => x.Value)
                     .Where(x => x.BlueMvp == playerId || x.RedMvp == playerId ||
                            x.HonoraryBlueOppMvp == playerId || x.HonoraryRedOppMvp == playerId))
                    .GroupBy(x => (x.TeamScheduleId, x.Game))
                    .ToDictionary(x => x.Key, x => x.FirstOrDefault());
                var mvpPoints = 0d;
                if (playerMvps.Any())
                {
                    var matches =
                        (matchDetails.SelectMany(x => x.Value).Where(x => x.PlayerId == playerId)).GroupBy(x => (x.TeamScheduleId, x.Game)).ToDictionary(
                            x => x.Key, x => x.FirstOrDefault());
                    mvpPoints = CalculateMvpPoints(playerMvps, matches, scheduleIds);
                }

                var mappedStat = _playerStatsMapper.MapForSeason(playerStat.Value, mvpPoints);
                mappedStats.Add(mappedStat);
            }

            return(mappedStats);
        }
        public async Task <PlayerProfileView> GetPlayerProfileAsync(Guid userId)
        {
            var seasonsTask      = _seasonInfoRepository.GetAllSeasonsAsync();
            var summonerTask     = _summonerInfoRepository.ReadOneByUserIdAsync(userId);
            var achievementsTask = _achievementRepository.GetAchievementsForUserAsync(userId);

            var summoner = await summonerTask;

            if (summoner == null)
            {
                var message = $"User {userId} was not found.";
                _logger.LogError(message);
                throw new SummonerInfoException($"User {userId} was not found.");
            }
            var achievements = await achievementsTask;

            var alternateAccountsTask = _alternateAccountRepository.ReadAllForSummonerAsync(summoner.Id);
            var teamPlayer            = (await _teamPlayerRepository.GetAllRostersForPlayerAsync(summoner.Id)).ToList();

            var seasons = (await seasonsTask).OrderBy(x => x.SeasonStartDate).ToList();

            var alternateAccounts = await alternateAccountsTask;
            var altAccountsMapped = _alternateAccountMapper.Map(alternateAccounts);
            var achievementViews  = new List <AchievementView>();

            foreach (var achievement in achievements)
            {
                var achievementView = new AchievementView(achievement);
                var seasonAchieved  = seasons.FirstOrDefault(x =>
                                                             x.SeasonStartDate <achievement.AchievedDate &&
                                                                                (x.SeasonEndDate ?? DateTime.MaxValue)> achievement.AchievedDate);
                achievementView.Season = seasonAchieved != null ? seasonAchieved.SeasonName : "PreSeason";
                achievementViews.Add(achievementView);
            }
            var playerStatsDictionary = new Dictionary <int, PlayerStatsView>();
            var matchDetails          = await _matchDetailRepository.GetMatchDetailsForPlayerAsync(new List <Guid> {
                summoner.Id
            });

            var statIds = matchDetails.Values.SelectMany(x => x.Select(y => y.PlayerStatsId));

            var allPlayerStats = await _playerStatsRepository.GetStatsAsync(statIds);

            var teamname = "None";

            if (teamPlayer.Any())
            {
                var latestSeason = seasons.Last();
                var team         = await _teamRosterRepository.GetTeamAsync(latestSeason.Id, teamPlayer.Select(x => x.TeamRosterId));

                if (team != null)
                {
                    teamname = team.TeamName;
                }
            }
            foreach (var seasonKvp in allPlayerStats)
            {
                var season      = seasons.First(x => x.Id == seasonKvp.Key.SeasonId);
                var seasonStats = allPlayerStats.SelectMany(x => x.Value.Where(y => y.SeasonInfoId == season.Id));
                var mappedStats = _playerStatsMapper.MapForSeason(seasonStats, 0);
                if (int.TryParse(season.SeasonName.Split(" ").Last(), out var seasonNum))
                {
                    playerStatsDictionary.Add(seasonNum, mappedStats);
                }
            }
            var view = new PlayerProfileView
            {
                PlayerName            = summoner.SummonerName,
                Rank                  = _tierDivisionMapper.Map(summoner.Tier_DivisionId),
                TeamName              = string.IsNullOrEmpty(teamname) ? "None" : teamname,
                Achievements          = achievementViews,
                PlayerStats           = playerStatsDictionary,
                AlternateAccountViews = altAccountsMapped
            };

            if (summoner.PreviousSeasonRankId != null)
            {
                view.PreviousSeasonRank = _tierDivisionMapper.Map(summoner.PreviousSeasonRankId.Value);
            }

            return(view);
        }