@model TournamentDto
@{
    ViewData["Title"] = Model.Name;
    var eventLevels = ViewBag.EventLevels as IEnumerable<TournamentEventLevelDto>;
    var events = ViewBag.Events as IEnumerable<EventDto>;
    var levels = ViewBag.Levels as IEnumerable<EventLevelDto>;
}

@{
    var statusClass = Model.Status switch {
        TournamentStatus.Draft => "bg-secondary",
        TournamentStatus.Registration => "bg-info",
        TournamentStatus.InProgress => "bg-primary",
        TournamentStatus.Completed => "bg-success",
        _ => "bg-dark"
    };
}
<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <h2 class="mb-1">
            @Model.Name
            <span class="badge @statusClass align-middle">@Model.Status</span>
            @if (Model.IsArchived) { <span class="badge bg-dark align-middle">Archived</span> }
        </h2>
        <p class="text-muted mb-0">@Model.StartDate.ToString("MMMM d") - @Model.EndDate.ToString("MMMM d, yyyy")</p>
    </div>
    <div class="d-flex gap-2 flex-wrap justify-content-end">
        @if (Model.Status == TournamentStatus.Draft)
        {
            <form asp-action="UpdateStatus" method="post">
                <input type="hidden" name="id" value="@Model.TournamentId" />
                <input type="hidden" name="status" value="Registration" />
                <button type="submit" class="btn btn-info">Open Registration</button>
            </form>
        }
        @if (Model.Status == TournamentStatus.Registration)
        {
            <form asp-action="UpdateStatus" method="post">
                <input type="hidden" name="id" value="@Model.TournamentId" />
                <input type="hidden" name="status" value="InProgress" />
                <button type="submit" class="btn btn-primary">Start Tournament</button>
            </form>
            <form asp-action="UpdateStatus" method="post">
                <input type="hidden" name="id" value="@Model.TournamentId" />
                <input type="hidden" name="status" value="Draft" />
                <button type="submit" class="btn btn-outline-secondary"
                        onclick="return confirm('Revert this tournament back to Draft?')">Back to Draft</button>
            </form>
        }
        @if (Model.Status == TournamentStatus.InProgress)
        {
            <form asp-action="UpdateStatus" method="post">
                <input type="hidden" name="id" value="@Model.TournamentId" />
                <input type="hidden" name="status" value="Completed" />
                <button type="submit" class="btn btn-success"
                        onclick="return confirm('Mark &quot;@Model.Name&quot; as Completed?\n\nYou can reopen it later if this was a mistake.')">Complete</button>
            </form>
            <form asp-action="UpdateStatus" method="post">
                <input type="hidden" name="id" value="@Model.TournamentId" />
                <input type="hidden" name="status" value="Registration" />
                <button type="submit" class="btn btn-outline-secondary"
                        onclick="return confirm('Move this tournament back to Registration?')">Back to Registration</button>
            </form>
        }
        @if (Model.Status == TournamentStatus.Completed)
        {
            <form asp-action="UpdateStatus" method="post">
                <input type="hidden" name="id" value="@Model.TournamentId" />
                <input type="hidden" name="status" value="InProgress" />
                <button type="submit" class="btn btn-warning"
                        onclick="return confirm('Reopen this completed tournament and set it back to In Progress?')">Reopen Tournament</button>
            </form>
        }
        <a asp-action="Edit" asp-route-id="@Model.TournamentId" class="btn btn-outline-primary">Edit</a>
        @if (Model.IsArchived)
        {
            <form asp-action="Unarchive" method="post">
                <input type="hidden" name="id" value="@Model.TournamentId" />
                <button type="submit" class="btn btn-outline-dark">Unarchive</button>
            </form>
        }
        else if (Model.Status == TournamentStatus.Completed)
        {
            <form asp-action="Archive" method="post">
                <input type="hidden" name="id" value="@Model.TournamentId" />
                <button type="submit" class="btn btn-outline-dark"
                        onclick="return confirm('Archive this tournament? It will be hidden from the default list but not deleted, and can be restored later.')">Archive</button>
            </form>
        }
    </div>
</div>

<partial name="_Notification" />

<div class="row">
    <div class="col-md-8">
        <div class="card shadow-sm mb-4">
            <div class="card-header d-flex justify-content-between align-items-center" style="background-color: #003366; color: white;">
                <h5 class="mb-0">Event Levels</h5>
            </div>
            <div class="card-body p-0">
                @if (eventLevels != null && eventLevels.Any())
                {
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr><th>Event</th><th>Level</th><th>Registrations</th><th>Waiver</th><th>Actions</th></tr>
                        </thead>
                        <tbody>
                            @foreach (var tel in eventLevels)
                            {
                                <tr>
                                    <td>@tel.EventName</td>
                                    <td>@tel.EventLevelName</td>
                                    <td>
                                        <a asp-controller="Registration" asp-action="Index" asp-route-tournamentEventLevelId="@tel.TournamentEventLevelId">
                                            @tel.RegistrationCount registrations
                                        </a>
                                    </td>
                                    <td>
                                        <form asp-action="ToggleAgeWaiver" method="post" class="d-inline">
                                            <input type="hidden" name="tournamentEventLevelId" value="@tel.TournamentEventLevelId" />
                                            <input type="hidden" name="id" value="@Model.TournamentId" />
                                            <button type="submit" class="btn btn-sm @(tel.AgeRestrictionWaived ? "btn-warning" : "btn-outline-secondary")">
                                                @(tel.AgeRestrictionWaived ? "Waived" : "Normal")
                                            </button>
                                        </form>
                                    </td>
                                    <td>
                                        @{
                                            var scoringController = tel.Category switch
                                            {
                                                EventCategory.Field => "FieldEvent",
                                                EventCategory.HighJump => "HighJump",
                                                _ => "TrackEvent"
                                            };
                                        }
                                        <a asp-controller="@scoringController" asp-action="Index" asp-route-tournamentEventLevelId="@tel.TournamentEventLevelId" class="btn btn-sm btn-outline-success">Scoring</a>
                                        <form asp-action="RemoveEventLevel" method="post" class="d-inline">
                                            <input type="hidden" name="tournamentEventLevelId" value="@tel.TournamentEventLevelId" />
                                            <input type="hidden" name="id" value="@Model.TournamentId" />
                                            <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Remove this event level?')">Remove</button>
                                        </form>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                }
                else
                {
                    <p class="text-muted p-3">No event levels configured. Add event levels using the form.</p>
                }
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card shadow-sm">
            <div class="card-header bg-dark text-white">
                <h6 class="mb-0">Add Event Level</h6>
            </div>
            <div class="card-body">
                <form asp-action="AddEventLevel" method="post">
                    <input type="hidden" name="TournamentId" value="@Model.TournamentId" />
                    <div class="mb-3">
                        <label class="form-label">Event</label>
                        <select name="EventId" class="form-select form-select-sm">
                            @if (events != null) { @foreach (var e in events) { <option value="@e.EventId">@e.Name (@e.Category)</option> } }
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Level</label>
                        <select name="EventLevelId" class="form-select form-select-sm">
                            @if (levels != null) { @foreach (var l in levels) { <option value="@l.EventLevelId">@l.Name</option> } }
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary btn-sm w-100">Add</button>
                </form>
            </div>
        </div>
    </div>
</div>
