@using System.Text.RegularExpressions
@using Core.Entities
@model Tuple<Team[], Student[], IDictionary<CompetitiveEvent, List<EventOccurrence>>>
@{
	ViewData["Title"] = "Teams";
	var eventOccurrences = Model.Item3;
}
<div class="container nobrk pt-5">
	<h2>Nationals Events</h2>
	<table class="table">
		<thead>
			<tr>
				<td>Team</td>
				<td>Team Members</td>
				<td>Dates</td>
				<td>Materials</td>

			</tr>
		</thead>
		@foreach (var team in Model.Item1)
		{
			var students = team.Students;
			@if (true @* team.Event.Format == EventFormat.Team *@)
			{
				<tr>
					<td>
						@* <strong class="@{ GetTeamClass(team);}">@team.Name</strong> *@
						@team.Name @* #@team.TeamNumber
 *@				</td>
					<td>
						@{
							var first = true;
						}
						@foreach (var student in students.OrderByDescending(s => (s.Grade + s.TsaYear) * (team.Captain == s ? 2 : 1)).ThenBy(s => s.FirstNameLastName))
						{
							@if (!first)
							{
								<text>, </text>

							}
							first = false;
							@student.FirstNameLastName @if (team.Captain == student)
							{
								<text> (Cpt.  @team.Captain.NationalID)</text>
							}
						}
					</td>
					<td>
						@* @if (team.Event.StatePresubmission)
						{
							<text>Pre-submission due Friday, March 14</text>
						}
						@if (team.Event.StatePretesting)
						{
							<text>Pre-testing Wednesday, April 2nd</text>
						}
						@if (team.Event.StatePreliminaryRound)
						{
							<text>Preliminary and Semifinalist Rounds</text>
						} *@
					</td>
					<td>
						@team.Event.Documentation
					</td>

				</tr>
			}
			@* 		else if (team.Event.Format == EventFormat.Individual)
		{
			foreach (var student in students)
			{
				<tr>
					<td>
						@team.Event.Name - @student.FirstNameLastName (@student.RegionalID)
					</td>
					<td>@team.RegionalTimeSlot</td>
					<td></td>
				</tr>
			}
		} *@
		}
	</table>
</div>

@{
	var s = Model.Item2.OrderBy(s => s.LastNameFirstName);

	@foreach (var student in s)
	{
		<div class="container nobrk pt-5" style="page-break-before: always;">

			<h3>@student.FirstNameLastName - @student.NationalID</h3>
			<h4>TSA 2025 Nationals Schedule</h4>
			<div class="row">
				<div class="col col-2">Events</div>
				<div class="col">
					@foreach (var ev in student.Teams)
					{
						<div class="row">

							<div class="col col-2">
								@if (ev.Event.Format is EventFormat.Team)
								{
									@ev.TeamNumber
								}
								else
								{
									@ev.Captain.NationalID
								}
							</div>
							<div class="col-4">
								<text> @ev.Name
									@if (ev.Captain == student)
									{
										<strong>(Cpt.)</strong>
									}
								</text>
							</div>
							<div class="col-6">
								@ev.Event.SemifinalistActivity
							</div>
						</div>
					}
				</div>
			</div>
			<table class="table">
				<thead>
					<tr>
						<td>Time</td>
						<td>Event</td>
						<td></td>
						<td>Location</td>
					</tr>
				</thead>

				@foreach (var date in
															eventOccurrences
																.Where(eo =>
																	student.Teams.Select(t => t.Event).Any(a => a == eo.Key)
																	|| eo.Key == CompetitiveEvent.GeneralSchedule
																	|| (eo.Key == CompetitiveEvent.VotingDelegates && student.VotingDelegate))
																.SelectMany(eo => eo.Value.Select(v => Tuple.Create(v, eo.Key)))
																.GroupBy(de => de.Item1.StartTime.Date)
																.OrderBy(d => d.Key)
														)
				{
					var eventsForThisDay =
						date
							.Where(de => de.Item1.Name != "Judging")
							.Where(de => de.Item1.StartTime > new DateTime(2024, 3, 1))
							// filter out occurrences where non-captain
							.Where(de =>
								!de.Item1.SignupSubmitPickup
								|| de.Item2.Format is EventFormat.Individual
								|| student.Teams.Any(t => t.Captain == student && t.Event == de.Item2)
							)
							.OrderBy(de => de.Item1.StartTime);

					@if (!eventsForThisDay.Any())
						continue;
					<tr>

						<td colspan="4" class="align-content-center text-center fw-bold">@date.Key.ToString("MMMM d")</td>
					</tr>
					@foreach (var eventOccurrence in eventsForThisDay.OrderBy(de => de.Item1.StartTime))
					{

						string hlClass = null;
						@if (!eventOccurrence.Item2.Name.Contains("General"))
						{
							hlClass = "fw-bold";
						}

						<tr>
							<td class="@hlClass" style="white-space:nowrap;">@eventOccurrence.Item1.Time</td>
							<td class="@hlClass">@eventOccurrence.Item2.Name</td>
							<td class="@hlClass">
							@eventOccurrence.Item1.Name
								@if (eventOccurrence.Item1.Name.Contains("Pick") && eventOccurrence.Item2.Format is EventFormat.Team)
								{
									<br/>
									<text>or coordinate with a teammate</text>
								}
							</td>
							<td>@eventOccurrence.Item1.Location
							@if (eventOccurrence.Item1.Location == "Online" && eventOccurrence.Item1.Name.Contains("Sign-up") ) { <text>by Advisor</text>}
							</td>
						</tr>
					}
				}

			</table>
		</div>
	}
}

@{
	<div class="container nobrk pt-5">
		<h2>Combined Schedule</h2>
	<table class="table">
		<thead>
		<tr>
			<td>Time</td>
			<td>Team</td>
			<td></td>
			<td>Location</td>
		</tr>
		</thead>
		<tbody>
		@foreach (var eventsForDate
		          in Model.Item3.SelectMany(eo => eo.Value.Select(e => Tuple.Create(e, eo.Key)))
			          .Where(de => de.Item1.Name != "Judging")
			          .GroupBy(eo => eo.Item1.StartTime.Date)
			          .OrderBy(eo => eo.Key)
		         )
		{
			<tr><td colspan="4"><strong>@eventsForDate.Key.ToString("MMMM d") </strong> </td></tr>
			@foreach (var occurrence in eventsForDate.OrderBy(o => o.Item1.StartTime))
			{
				var teams = Model.Item1.Where(t => t.Event == occurrence.Item2);
				if (occurrence.Item2 != CompetitiveEvent.GeneralSchedule && occurrence.Item2 != CompetitiveEvent.VotingDelegates && !teams.Any())
					continue;
				<tr>
							<td style="white-space:nowrap;">@occurrence.Item1.Time</td>
					<td>
						@if (occurrence.Item2 == CompetitiveEvent.GeneralSchedule)
						{
							<text>Everyone</text>
						}
						else if (occurrence.Item2 == CompetitiveEvent.VotingDelegates)
						{
							<text>Voting Delegates - @string.Join(", ", Model.Item2.Where(stu => stu.VotingDelegate).Select(stu => stu.FirstName))</text>
						}
						@foreach (var team in teams)
						{
							<text>@team</text>


							<text> - @string.Join(", ", team.Students.Select(stu => stu.FirstName))</text>
						}

					</td>
					<td>
						@occurrence.Item1.Name
						
						@if (occurrence.Item1.SignupSubmitPickup)
						{
							<br/>
							<text>1 Team Member</text>
						}

						@if (occurrence.Item1.Name.Contains("Semifinalist") && (occurrence.Item1.Name.Contains("Interview") || occurrence.Item1.Name.Contains("Presentation")))
						{
							<br/>
									<text>@Regex.Match(@occurrence.Item2.SemifinalistActivity, @"(?<=\().*?(?=\))").Value</text>
						}
					</td>
					<td>@occurrence.Item1.Location</td>
				</tr>
			}
		}
		</tbody>
	</table>
</div>
}

						
<div class="container nobrk pt-5">
	<h2>Students</h2>
	<table class="table-primary">
		<thead>
		<tr>
			<td>Student</td>
			<td>ID</td>
		</tr>
		</thead>
		@foreach (var student in Model.Item2.OrderByDescending(s => s.Officer.Contains("President")).ThenBy(s => s.LastNameFirstName))
		{
			var assignments
				= student.Teams.Where(ea => ea.Students.Contains(student))
		
					.Distinct()
					.OrderBy(e => e.Name).ToList();

			<tr>
				<td>
					<strong>@student.FirstNameLastName</strong> @if (!string.IsNullOrEmpty(student.Officer))
					                                            {
						                                            <text>(@student.Officer)</text>
					                                            }
				</td>
				<td>@student.NationalID</td>

				@foreach (var t in assignments)
				{
					<td>
						@if (t.Event.Format != EventFormat.Individual)
						{
							@t.Name
							@if (t.Captain == student)
							{
								<text> (Captain)</text>
							}
						}
						else
						{
							@t.Event.Name
						}
					</td>
				}
			</tr>
		}
	</table>
</div>

						
<div class="container nobrk pt-5">
	<h2>Students</h2>
		@foreach (var student in Model.Item2.OrderByDescending(s => s.Officer.Contains("President")).ThenBy(s => s.LastNameFirstName))
		{
			var assignments
			= student.Teams.Where(ea => ea.Students.Contains(student))

			.Distinct()
			.OrderBy(e => e.Name).ToList();

				<p>
					<strong>@student.FirstNameLastName</strong> 
					@if (!string.IsNullOrEmpty(student.Officer))
					{
						<text>(@student.Officer)</text>
					}
			<text>@student.NationalID</text>
				</p>
				

				@foreach (var t in assignments)
				{
					<p>
						@if (t.Event.Format != EventFormat.Individual)
						{
							@t.Name
							@if (t.Captain == student)
							{
								<text> (Captain)</text>
							}
						}
						else
						{
							@t.Event.Name
						}
					</p>
				}
		}
</div>