@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">
    <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 @onsubmit="UpdateGuestAccessCode" @onsubmit:preventDefault="true">
                <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" @bind="NewGuestAccessCode" />
                </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">
        <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 @onsubmit="CreateMaintainer" @onsubmit:preventDefault="true">
                <div class="mb-3">
                    <label class="form-label" for="maintainer-username">Username</label>
                    <input id="maintainer-username" class="form-control" @bind="MaintainerUsername" />
                </div>

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

                <div class="mb-3">
                    <label class="form-label" for="maintainer-password">Password</label>
                    <input id="maintainer-password" class="form-control" type="password" @bind="MaintainerPassword" />
                </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>

    <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>
                            <button type="button" class="btn btn-outline-danger btn-sm" @onclick="() => DeleteMaintainer(maintainer.Id)">
                                Delete
                            </button>
                        </div>
                    }
                </div>
            }
        </div>
    </section>

    <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>

            <button type="button" class="btn btn-outline-primary" @onclick="CompleteSetupRecovery">
                Complete setup recovery
            </button>

            @if (!string.IsNullOrWhiteSpace(RecoveryMessage))
            {
                <div class="alert alert-info mt-3 mb-0" role="alert">@RecoveryMessage</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">Move existing users to backup and mark setup as incomplete.</p>

            <button type="button" class="btn btn-outline-danger" @onclick="ResetSetup">
                Reset setup
            </button>

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