@model IEnumerable<TournamentDto>
@{
    ViewData["Title"] = "Tournaments";
    var selectedStatus = ViewBag.SelectedStatus as TournamentStatus?;
    var showArchived = ViewBag.ShowArchived as bool? ?? false;
    var archivedCount = ViewBag.ArchivedCount as int? ?? 0;
}

<div class="d-flex justify-content-between align-items-center mb-3">
    <h2>Tournaments</h2>
    <a asp-action="Create" class="btn btn-primary">Create Tournament</a>
</div>

<partial name="_Notification" />

<form asp-action="Index" method="get" class="row g-2 align-items-end mb-3">
    <div class="col-auto">
        <label class="form-label mb-0 small text-muted">Status</label>
        <select name="status" class="form-select form-select-sm" onchange="this.form.submit()">
            <option value="">All statuses</option>
            @foreach (var s in Enum.GetValues<TournamentStatus>())
            {
                <option value="@s" selected="@(selectedStatus == s)">@s</option>
            }
        </select>
    </div>
    <div class="col-auto">
        <label class="form-label mb-0 small text-muted d-block">Archived</label>
        <div class="form-check form-switch d-inline-flex align-items-center gap-2 border rounded px-2 py-1 @(showArchived ? "border-primary bg-light" : (archivedCount > 0 ? "border-warning" : ""))" style="min-height:31px">
            <input class="form-check-input m-0" type="checkbox" role="switch" name="showArchived" value="true"
                   id="showArchived" @(showArchived ? "checked" : "") onchange="this.form.submit()" />
            <label class="form-check-label fw-semibold" for="showArchived" style="cursor:pointer">
                Show archived
                @if (archivedCount > 0)
                {
                    <span class="badge rounded-pill bg-warning text-dark" title="@archivedCount archived tournament(s) hidden">@archivedCount</span>
                }
            </label>
        </div>
    </div>
</form>

@if (!showArchived && archivedCount > 0)
{
    <p class="text-muted small mb-3">
        <i class="bi bi-archive"></i>
        @archivedCount archived tournament@(archivedCount == 1 ? "" : "s") @(archivedCount == 1 ? "is" : "are") hidden.
        Flip <strong>Show archived</strong> above to view @(archivedCount == 1 ? "it" : "them").
    </p>
}

<table class="table table-striped table-hover">
    <thead class="table-dark">
        <tr>
            <th>Name</th>
            <th>Dates</th>
            <th>Status</th>
            <th>Events</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var t in Model)
        {
            <tr>
                <td>
                    <a asp-action="Details" asp-route-id="@t.TournamentId" class="text-decoration-none fw-bold">@t.Name</a>
                    @if (t.IsArchived) { <span class="badge bg-secondary ms-1">Archived</span> }
                </td>
                <td>@t.StartDate.ToString("MMM d") - @t.EndDate.ToString("MMM d, yyyy")</td>
                <td>
                    @{
                        var statusClass = t.Status switch {
                            TournamentStatus.Draft => "bg-secondary",
                            TournamentStatus.Registration => "bg-info",
                            TournamentStatus.InProgress => "bg-primary",
                            TournamentStatus.Completed => "bg-success",
                            _ => "bg-dark"
                        };
                    }
                    <span class="badge @statusClass">@t.Status</span>
                </td>
                <td>@t.EventCount</td>
                <td>
                    <a asp-action="Edit" asp-route-id="@t.TournamentId" class="btn btn-sm btn-outline-primary">Edit</a>
                    <a asp-action="Details" asp-route-id="@t.TournamentId" class="btn btn-sm btn-outline-info">Details</a>
                    @if (t.IsArchived)
                    {
                        <form asp-action="Unarchive" method="post" class="d-inline">
                            <input type="hidden" name="id" value="@t.TournamentId" />
                            <button type="submit" class="btn btn-sm btn-outline-dark">Unarchive</button>
                        </form>
                    }
                    else if (t.Status == TournamentStatus.Completed)
                    {
                        <form asp-action="Archive" method="post" class="d-inline">
                            <input type="hidden" name="id" value="@t.TournamentId" />
                            <button type="submit" class="btn btn-sm btn-outline-dark"
                                    onclick="return confirm('Archive &quot;@t.Name&quot;? It will be hidden from the default list but can be restored.')">Archive</button>
                        </form>
                    }
                </td>
            </tr>
        }
    </tbody>
</table>

<partial name="_Pagination" />
