@page "/teams/handout"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using WebApp.Models
@inject IConfiguration Configuration
@inject AppDbContext Context

<PageTitle>@Configuration["ChapterSettings:Shortname"] TSA Teams @Configuration["ChapterSettings:CompetitionYear"]</PageTitle>

<MudText Typo="Typo.h3">@Configuration["ChapterSettings:Shortname"] TSA Teams @Configuration["ChapterSettings:CompetitionYear"]</MudText>
<MudText Typo="Typo.h5" Class="mb-4">Yearly theme: Unity Through Community</MudText>

@if (_teams == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <MudContainer>
        @foreach (var studentForEvents in _students)
        {
            <MudContainer Class="pagebreak">
                <MudText Typo="Typo.h4">@studentForEvents.Name</MudText>
                @foreach (var team in studentForEvents.Teams)
                {
                    <MudContainer Class="mt-3 mb-1 nobrk">
                        <MudGrid>
                            <MudItem xs="6">
                                <MudStack>
                                    <MudItem>
                                        <MudText Class="d-flex py-1" Typo="Typo.h5">
                                            @team.ToString()
                                        </MudText>
                                    </MudItem>

                                    @if (team.Event.RegionalEvent)
                                    {
                                        <MudItem>
                                            <MudText Class="d-flex" Typo="Typo.caption"><i>Regional Event</i></MudText>
                                        </MudItem>
                                    }
                                </MudStack>
                            </MudItem>
                            <MudItem xs="2">
                                <MudText>
                                    <strong>@team.Event.EventFormat</strong>
                                </MudText>

                            </MudItem>
                            <MudItem xs="1">
                                <strong>Effort</strong>: @AppIcons.LevelOfEffortIcon(@team.Event.LevelOfEffort)
                            </MudItem>
                            <MudItem xs="3">
                                <strong>Activity</strong>: @team.Event.SemifinalistActivity
                            </MudItem>
                            @if (team.Event.EventFormat == EventFormat.Team)
                            {
                                <MudItem xs="12">
                                    <MudText Class="d-flex py-1" Typo="Typo.h6">
                                        Team Members: @string.Join(", ", team.Students.OrderByDescending(e => e.Grade + e.TsaYear).Select(e => e.FirstName))
                                    </MudText>
                                </MudItem>
                            }
                            <MudItem xs="12">
                                <MudText Class="d-flex py-1" Style="white-space:pre-wrap;">@team.Event.Description</MudText>
                            </MudItem>
                            @if (!string.IsNullOrEmpty(team.Event.Theme))
                            {
                                <MudItem xs="3">
                                    <MudText Class="d-flex py-1">
                                        <i>Theme for 2025-26:</i>
                                    </MudText>
                                </MudItem>
                                <MudItem xs="8">
                                    <MudText Class="d-flex py-1" Style="white-space:pre-wrap;">@team.Event.Theme</MudText>
                                </MudItem>
                            }

                            @if (!string.IsNullOrEmpty(team.Event.Documentation))
                            {
                                <MudItem xs="3">
                                    <MudText Class="d-flex py-1">
                                        <i>Materials:</i>
                                    </MudText>
                                </MudItem>
                                <MudItem xs="8">
                                    <MudText Class="d-flex py-1" Style="white-space:pre-wrap;">@team.Event.Documentation</MudText>
                                </MudItem>
                            }

                        </MudGrid>
                    </MudContainer>
                    <MudDivider/>
                }
            </MudContainer>
        }
    </MudContainer>
}
@code {
    private Team[]? _teams;
    private int _maxTeamSize;
    private Student[]? _students;

    protected override async Task OnInitializedAsync()
    {
        _teams
                = await Context.Teams
                    .Include(e => e.Event)
                    .Include(e => e.Students)
                    .OrderBy(e => e.Event.Name)
                    .ThenBy(e => e.Identifier)
                    .ToArrayAsync();

        _maxTeamSize = _teams.Max(t => t.Students.Count);
        _students = 
            await Context.Students
                .Include(e => e.Teams)
                .ThenInclude(e => e.Captain)
                .Include(e => e.EventRankings)
                .ThenInclude(e => e.EventDefinition)
                .OrderBy(e => e.FirstName).ToArrayAsync();
    }
}
