@page "/admin"
@rendermode InteractiveServer
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize(Roles = "Admin")]

<div class="mb-4">
    <h1 class="h2 mb-1">Admin</h1>
    <p class="text-secondary mb-0">Manage access, maintainers and setup recovery.</p>
</div>

<div class="admin-panel-grid">
    <div class="admin-panel-column">
        <section class="card shadow-sm">
            <div class="card-body">
                <h2 class="h5 card-title">Guest access</h2>
                <p class="text-secondary">Change the code guests use to view the list.</p>

                <form method="post" action="/admin/guest-access-code">
                    <div class="mb-3">
                        <label class="form-label" for="guest-access-code">New guest access code</label>
                        <input id="guest-access-code" class="form-control" name="guestAccessCode" />
                    </div>

                    <button type="submit" class="btn btn-primary">
                        Update guest access code
                    </button>
                </form>

                @if (!string.IsNullOrWhiteSpace(AccessCodeMessage))
                {
                    <div class="alert alert-info mt-3 mb-0" role="alert">@AccessCodeMessage</div>
                }
            </div>
        </section>

        <section class="card shadow-sm border-danger-subtle">
            <div class="card-body">
                <h2 class="h5 card-title">Reset setup</h2>
                <p class="text-secondary">Copy the current database to a timestamped backup database and start setup with a fresh database.</p>

                <form method="post" action="/admin/reset-setup">
                    <button type="submit" class="btn btn-outline-danger">
                        Reset setup
                    </button>
                </form>

                @if (!string.IsNullOrWhiteSpace(Message))
                {
                    <div class="alert alert-warning mt-3 mb-0" role="alert">@Message</div>
                }
            </div>
        </section>
    </div>

    <div class="admin-panel-column">
        <section class="card shadow-sm">
            <div class="card-body">
                <h2 class="h5 card-title">Create maintainer</h2>
                <p class="text-secondary">Maintainers can add, edit and delete guests.</p>

                <form method="post" action="/admin/maintainers/create">
                    <div class="mb-3">
                        <label class="form-label" for="maintainer-username">Username</label>
                        <input id="maintainer-username" class="form-control" name="username" />
                    </div>

                    <div class="mb-3">
                        <label class="form-label" for="maintainer-name">Name</label>
                        <input id="maintainer-name" class="form-control" name="displayName" />
                    </div>

                    <div class="mb-3">
                        <label class="form-label" for="maintainer-password">Password</label>
                        <input id="maintainer-password" class="form-control" type="password" name="password" />
                    </div>

                    <button type="submit" class="btn btn-primary">
                        Create maintainer
                    </button>
                </form>

                @if (!string.IsNullOrWhiteSpace(MaintainerMessage))
                {
                    <div class="alert alert-info mt-3 mb-0" role="alert">@MaintainerMessage</div>
                }
            </div>
        </section>
    </div>

    <div class="admin-panel-column">
        <section class="card shadow-sm">
            <div class="card-body">
                <h2 class="h5 card-title">Maintainers</h2>

                @if (Maintainers is null)
                {
                    <p class="text-secondary mb-0">Loading maintainers...</p>
                }
                else if (Maintainers.Count == 0)
                {
                    <p class="text-secondary mb-0">No maintainers have been created yet.</p>
                }
                else
                {
                    <div class="list-group list-group-flush">
                        @foreach (var maintainer in Maintainers)
                        {
                            <div class="list-group-item px-0 d-flex align-items-center justify-content-between gap-3">
                                <div>
                                    <div class="fw-semibold">@maintainer.DisplayName</div>
                                    <div class="small text-secondary">@maintainer.Username</div>
                                </div>
                                <form method="post" action="/admin/maintainers/delete">
                                    <input type="hidden" name="id" value="@maintainer.Id" />
                                    <button type="submit" class="btn btn-outline-danger btn-sm">
                                        Delete
                                    </button>
                                </form>
                            </div>
                        }
                    </div>
                }
            </div>
        </section>
    </div>

    <div class="admin-panel-column">
        <section class="card shadow-sm">
            <div class="card-body">
                <h2 class="h5 card-title">Setup recovery</h2>
                <p class="text-secondary">Use this after a partial setup if admin access exists but setup is incomplete.</p>

                <form method="post" action="/admin/setup-recovery">
                    <button type="submit" class="btn btn-outline-primary">
                        Complete setup recovery
                    </button>
                </form>

                @if (!string.IsNullOrWhiteSpace(RecoveryMessage))
                {
                    <div class="alert alert-info mt-3 mb-0" role="alert">@RecoveryMessage</div>
                }
            </div>
        </section>
    </div>
</div>
