@using Core.Entities

@model Tuple<IList<Team>[], IEnumerable<Student>[], Web.Controllers.HomeController.ScheduleOptions>

@{
	ViewData["Title"] = "Schedule";
	var slot = 0;

	var schedule = Model.Item1;
	var unassignedStudents = Model.Item2;
	var allStudents = schedule.SelectMany(t => t).SelectMany(t => t.Students).Distinct().ToList();
	var allMeetingTeams = schedule.SelectMany(t => t).Distinct();
}

 <div class="bluewhite p-5"> 
	@{
		List<Tuple<Student, IEnumerable<Team>>> overlaps;
	}
	@foreach (var timeslot in schedule)
	{
		overlaps = Team.GetStudentTeamOverlaps(timeslot).ToList();
		var partialTeams = timeslot.Where(t => t is PartialTeam && t.Event.Format is not EventFormat.Individual);
		var fullTeams = timeslot.Where(t => !partialTeams.Contains(t));

		<h3>Time Slot @(slot + 1)</h3>
		<p>@Model.Item3.Date.ToShortDateString()</p>

		<h4>Teams</h4>
		<table class="table schedule">
			<thead>
			<th class="col-2"></th>
			<th class="col-3"></th>
			<th class="col-5"></th>
			</thead>
			<tbody>
			@foreach (var team in fullTeams.Where(t => t.Event.Format is EventFormat.Team).OrderBy(t => t.Name))
			{
					@await Html.PartialAsync("ScheduleTeamPartial", Tuple.Create(team, overlaps, Model.Item3.AbsentStudents))
			}
			</tbody>
		</table>

		if (partialTeams.Any())
		{
			<table class="table schedule">
				<thead>
				<th class="col-2"></th>
				<th class="col-3"></th>
				<th class="col-5"></th>
				</thead>
				<tbody>
				@foreach (var team in partialTeams.OrderBy(t => t.Name))
				{
					@await Html.PartialAsync("ScheduleTeamPartial", Tuple.Create(team, overlaps, Model.Item3.AbsentStudents))
				}
				</tbody>
			</table>
		}

		<h4>Individual</h4>
		<p><i>Use time for individual event or to <strong>work on a team event</strong> </i></p>
		<table class="table schedule">
			<tbody>

			<tr>

			@foreach (var team in fullTeams.Where(t => t.Event.Format is EventFormat.Individual).OrderBy(t => t.Name))
			{
				<td>
					<strong>@team.Captain?.FirstName</strong>  (@team.Event.Name@if(team.Event.RegionalEvent){ @* <text> (<i>Regional</i>)</text>  *@})
				</td>
@* 				@await Html.PartialAsync("ScheduleTeamPartial", Tuple.Create(team, overlaps)) *@
			}
			
			@foreach (var student in unassignedStudents[slot])
			{
				<td>
					<strong>@student.FirstName</strong>
				</td>
			}
			</tr>
			</tbody>
	     </table>


		slot++;
	}

	<h3>Missed team events for today</h3>
	<table class="table schedule">
		@foreach (var student in allStudents.OrderBy(s => s.FirstName))
		{
			var studentMeetings = student.Teams.Where(t => allMeetingTeams.Any(mt => mt.Name == t.Name && mt.Students.Contains(student)));
			var studentMissed = student.Teams.Where(t => studentMeetings.All(mt => mt.Name != t.Name)).ToList();
			var studentMissedTeams = studentMissed.Where(t => t.Event.Format is EventFormat.Team);
			var studentMissedIndividual = studentMissed.Where(t => t.Event.Format is EventFormat.Individual);
			if (studentMissedTeams.Any())
			{
				<tr>
					<td class="col-4">@student.FirstName</td>
					<td>
						@string.Join(", ", studentMissedTeams.Select(t => t.ToStringWithIndividualAndRegional()))
					</td>
				</tr>
			}
			if (studentMissedIndividual.Any())
			{
				<tr>
					<td class="col-4">@student.FirstName</td>
					<td>
						@string.Join(", ", studentMissedIndividual.Select(t => t.ToStringWithIndividualAndRegional()))
					</td>
				</tr>
			}
		}
	</table>
</div>


@functions
{
	private void GetTeamClass(Team team)
	{
		if (team.Event.RegionalEvent)
		{
			@Html.Raw("regional")
			;
		}
	}
}
