@model DashboardDto
@{
    ViewData["Title"] = "Dashboard";
    var tournamentList = (ViewBag.Tournaments as IEnumerable<TournamentDto>)?.ToList() ?? new List<TournamentDto>();
    var selectedTournamentId = ViewBag.SelectedTournamentId as int?;
    var selectedTournament = tournamentList.FirstOrDefault(t => t.TournamentId == selectedTournamentId);
}

<div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
    <h2 class="mb-0">Dashboard</h2>
    @if (tournamentList.Count > 1)
    {
        <form method="get" class="d-flex align-items-center gap-2">
            <label class="form-label mb-0 text-nowrap">Tournament:</label>
            <select name="tournamentId" class="form-select form-select-sm" style="width:250px" onchange="this.form.submit()">
                <option value="">-- Select a tournament --</option>
                @foreach (var t in tournamentList)
                {
                    <option value="@t.TournamentId" selected="@(selectedTournamentId == t.TournamentId)">@t.Name</option>
                }
            </select>
        </form>
    }
    else if (tournamentList.Count == 1)
    {
        <span class="badge bg-primary fs-6"><i class="bi bi-trophy me-1"></i>@tournamentList[0].Name</span>
    }
</div>

@* ---- Top-level counts (system-wide) ---- *@
<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="card bg-primary text-white shadow-sm h-100">
            <div class="card-body">
                <h6 class="card-title"><i class="bi bi-people me-1"></i>Total Students</h6>
                <h2 class="mb-0">@Model.TotalStudents</h2>
                <small>Male: @Model.MaleStudents | Female: @Model.FemaleStudents</small>
            </div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card bg-success text-white shadow-sm h-100">
            <div class="card-body">
                <h6 class="card-title"><i class="bi bi-building me-1"></i>Schools</h6>
                <h2 class="mb-0">@Model.TotalSchools</h2>
                <small>Participating institutions</small>
            </div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card bg-info text-white shadow-sm h-100">
            <div class="card-body">
                <h6 class="card-title"><i class="bi bi-calendar-event me-1"></i>Events</h6>
                <h2 class="mb-0">@Model.TotalEvents</h2>
                <small>Track, field &amp; high jump</small>
            </div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card bg-dark text-white shadow-sm h-100">
            <div class="card-body">
                <h6 class="card-title"><i class="bi bi-clipboard-check me-1"></i>Scores Recorded</h6>
                <h2 class="mb-0">@Model.TotalScores</h2>
                <small>Across all tournaments</small>
            </div>
        </div>
    </div>
</div>

@* ---- Tournament status breakdown + quick actions ---- *@
<div class="row g-3 mb-4">
    <div class="col-md-7">
        <div class="card shadow-sm h-100">
            <div class="card-header" style="background-color:#003366; color:white;">
                <h5 class="mb-0"><i class="bi bi-trophy me-1"></i>Tournaments by Status</h5>
            </div>
            <div class="card-body">
                <div class="d-flex flex-wrap gap-3">
                    <div class="text-center">
                        <div class="h3 mb-0">@Model.DraftTournaments</div>
                        <span class="badge bg-secondary">Draft</span>
                    </div>
                    <div class="text-center">
                        <div class="h3 mb-0">@Model.RegistrationTournaments</div>
                        <span class="badge bg-info">Registration</span>
                    </div>
                    <div class="text-center">
                        <div class="h3 mb-0">@Model.ActiveTournaments</div>
                        <span class="badge bg-primary">In Progress</span>
                    </div>
                    <div class="text-center">
                        <div class="h3 mb-0">@Model.CompletedTournaments</div>
                        <span class="badge bg-success">Completed</span>
                    </div>
                    <div class="text-center">
                        <div class="h3 mb-0">@Model.ArchivedTournaments</div>
                        <span class="badge bg-dark">Archived</span>
                    </div>
                </div>
                <hr />
                <a asp-controller="Tournament" asp-action="Index" class="btn btn-sm btn-outline-primary">
                    <i class="bi bi-list-ul me-1"></i>Manage tournaments
                </a>
            </div>
        </div>
    </div>
    <div class="col-md-5">
        <div class="card shadow-sm h-100">
            <div class="card-header bg-dark text-white">
                <h5 class="mb-0"><i class="bi bi-lightning-charge me-1"></i>Quick Actions</h5>
            </div>
            <div class="card-body d-flex flex-wrap gap-2 align-content-start">
                <a asp-controller="Tournament" asp-action="Create" class="btn btn-outline-primary btn-sm"><i class="bi bi-plus-circle me-1"></i>New Tournament</a>
                <a asp-controller="School" asp-action="Index" class="btn btn-outline-secondary btn-sm"><i class="bi bi-building me-1"></i>Schools</a>
                <a asp-controller="Student" asp-action="Index" class="btn btn-outline-secondary btn-sm"><i class="bi bi-people me-1"></i>Students</a>
                <a asp-controller="Report" asp-action="Index" class="btn btn-outline-success btn-sm"><i class="bi bi-file-earmark-bar-graph me-1"></i>Reports</a>
                @if (User.IsInRole("Admin"))
                {
                    <a asp-controller="ScoringConfig" asp-action="Index" class="btn btn-outline-dark btn-sm"><i class="bi bi-sliders me-1"></i>Scoring Config</a>
                }
                <a asp-controller="Help" asp-action="Index" class="btn btn-outline-info btn-sm"><i class="bi bi-question-circle me-1"></i>Help</a>
            </div>
        </div>
    </div>
</div>

@if (selectedTournamentId.HasValue)
{
    @* ---- Selected-tournament detail ---- *@
    <h4 class="mb-3"><i class="bi bi-bullseye me-1"></i>@(selectedTournament?.Name ?? "Selected Tournament")</h4>
    <div class="row g-3 mb-4">
        <div class="col-6 col-md-4">
            <div class="card bg-warning text-dark shadow-sm h-100">
                <div class="card-body">
                    <h6 class="card-title"><i class="bi bi-card-checklist me-1"></i>Registrations</h6>
                    <h2 class="mb-0">@Model.TotalRegistrations</h2>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4">
            <div class="card bg-success text-white shadow-sm h-100">
                <div class="card-body">
                    <h6 class="card-title"><i class="bi bi-check2-circle me-1"></i>Events Scored</h6>
                    <h2 class="mb-0">@Model.EventsCompleted</h2>
                    <small>Results calculated</small>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4">
            <div class="card bg-secondary text-white shadow-sm h-100">
                <div class="card-body">
                    <h6 class="card-title"><i class="bi bi-hourglass-split me-1"></i>Events Pending</h6>
                    <h2 class="mb-0">@Model.EventsInProgress</h2>
                    <small>Awaiting results</small>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-3">
        <div class="col-lg-7">
            <div class="card shadow-sm h-100">
                <div class="card-header" style="background-color:#003366; color:white;">
                    <h5 class="mb-0"><i class="bi bi-bar-chart-line me-1"></i>School Standings</h5>
                </div>
                <div class="card-body p-0">
                    @if (Model.TopSchools.Any())
                    {
                        <table class="table table-striped table-hover mb-0">
                            <thead class="table-dark">
                                <tr>
                                    <th>#</th>
                                    <th>School</th>
                                    <th class="text-center">Points</th>
                                    <th class="text-center">1st</th>
                                    <th class="text-center">2nd</th>
                                    <th class="text-center">3rd</th>
                                </tr>
                            </thead>
                            <tbody>
                                @for (int i = 0; i < Model.TopSchools.Count; i++)
                                {
                                    var school = Model.TopSchools[i];
                                    <tr>
                                        <td><strong>@(i + 1)</strong></td>
                                        <td>@school.SchoolName @(school.ShortName != null ? $"({school.ShortName})" : "")</td>
                                        <td class="text-center"><strong>@school.TotalPoints</strong></td>
                                        <td class="text-center">@school.FirstPlaceCount</td>
                                        <td class="text-center">@school.SecondPlaceCount</td>
                                        <td class="text-center">@school.ThirdPlaceCount</td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    }
                    else
                    {
                        <p class="text-muted p-3 mb-0">No points scored yet for this tournament.</p>
                    }
                </div>
            </div>
        </div>
        <div class="col-lg-5">
            <div class="card shadow-sm h-100">
                <div class="card-header bg-dark text-white">
                    <h5 class="mb-0"><i class="bi bi-activity me-1"></i>Recent Activity</h5>
                </div>
                <div class="card-body p-0">
                    @if (Model.RecentScores.Any())
                    {
                        <ul class="list-group list-group-flush">
                            @foreach (var s in Model.RecentScores)
                            {
                                <li class="list-group-item d-flex justify-content-between align-items-start">
                                    <div>
                                        <div class="fw-semibold">@s.StudentName</div>
                                        <small class="text-muted">@s.EventName — @s.EventLevelName · @s.RawPerformance.ToString("0.00")</small>
                                    </div>
                                    <div class="text-end text-nowrap">
                                        @if (s.Placement != null)
                                        {
                                            <span class="badge bg-warning text-dark">#@s.Placement</span>
                                        }
                                        <div><small class="text-muted">@s.RecordedAt.ToString("MMM d, HH:mm")</small></div>
                                    </div>
                                </li>
                            }
                        </ul>
                    }
                    else
                    {
                        <p class="text-muted p-3 mb-0">No scores recorded yet for this tournament.</p>
                    }
                </div>
            </div>
        </div>
    </div>
}
else if (tournamentList.Count > 1)
{
    <div class="alert alert-info">
        <i class="bi bi-info-circle me-1"></i>
        You have multiple active tournaments. <strong>Select one above</strong> to see its registrations, school standings, and recent scoring activity.
    </div>
}
else
{
    <div class="alert alert-secondary">
        <i class="bi bi-info-circle me-1"></i>
        No active tournaments yet. <a asp-controller="Tournament" asp-action="Create">Create a tournament</a> to get started.
    </div>
}
