using Core.Calculation;
using Core.Entities;
using Tests.Parsers;

namespace Tests.Calculation;

[TestFixture]
public class TeamSchedulerTest
{
	[Test]
	public void Prototype_Test()
	{
		var teamSchedulerTest = new TeamScheduler_Prototype();
		teamSchedulerTest.Solve();
	}

	[Test]
	public void SolutionTest()
	{
		var events = TestEntityHandler.GetEvents();
		var students = TestEntityHandler.GetStudents(events);
		
		var allTeams = TestEntityHandler.GetTeams(events, students);
		var teams = allTeams;

		teams =
			(from e in events
			 from t in teams
			 where t.Event == e
				      //&& t.Students.Count > 1
				      && (e.EventFormat == EventFormat.Team && e.RegionalEvent)
				select t).ToArray();
		teams =
			teams.Where(t => !t.Event.Name.Contains("Tech Bowl")).ToArray();

		//var eventAssignment = new EventAssignment(events, students);
		//var teams = eventAssignment.Solve();

		TeamSchedulerSolution solution;
		if (true)
		{
			var teamScheduler = TeamScheduler.CreateInstance(teams, 3);
			solution = teamScheduler.Solve();
		}
		else
		{
			var teamScheduler = new TeamScheduler_DecisionTree(teams, 3);
			solution = teamScheduler.Solve();
		}

		solution = new UnassignedStudentScheduler(allTeams, solution.TimeSlots).ScheduleStrategy(UnassignedScheduleStrategy.BiggestGroup);
		solution = new UnassignedStudentScheduler(allTeams, solution.TimeSlots).ScheduleStrategy(UnassignedScheduleStrategy.IndividualEvents);

		var i = 1;
		foreach (var slot in solution.TimeSlots)
		{
			Console.WriteLine($"Time slot {i++}");
			foreach (var team in slot.OrderBy(s => s.Event.Name))
			{
				var names = string.Join(", ", team.Students.OrderByDescending(s => s.Grade + s.TsaYear).Select(s => s.FirstName));
				Console.WriteLine($"\t{team.Event.Name}");
				Console.WriteLine($"\t\t{names}");
			}

			var overlaps = TeamSchedulerSolution.GetStudentTeamOverlaps(slot).ToList();

			if (overlaps.Any())
			{
				Console.WriteLine("\toverlaps");
				foreach (var overlap in overlaps)
					Console.WriteLine(
						$"\t\t{overlap.Item1.Name} : {string.Join(", ", overlap.Item2.Select(t => t.Event.Name))}");
			}

			var unassigned = UnassignedStudentScheduler.UnassignedStudents(students, slot).ToList();
			
			if (unassigned.Any())
				Console.WriteLine("\tunassigned");
			Console.WriteLine($"\t\t{string.Join(", ", unassigned.Select(s => s.FirstName))}");
		}

		//var allScheduledTeams = timeSlots.SelectMany(list => list).GroupBy(t => t.Name).SelectMany(g => g).Distinct();
		//foreach (var allScheduledTeam in allScheduledTeams.OrderBy(a => a.Name))
		//{
		//	Console.WriteLine($"{allScheduledTeam.Name}");
		//	Console.WriteLine($"\t{string.Join(", ", allScheduledTeam.Students.Select(s => s.FirstName))}");
		//}
	}
}