@page "/setup"
@rendermode InteractiveServer

<div class="mb-4">
    <h1 class="h2 mb-1">Setup</h1>
    <p class="text-secondary mb-0">Create the first admin and initial guest access code.</p>
</div>

@if (IsSetupComplete is null || HasAnyUsers is null || HasAnyDatabaseBackups is null)
{
    <section class="card shadow-sm">
        <div class="card-body">
            <p class="text-secondary mb-0">Loading setup status...</p>

            @if (!string.IsNullOrWhiteSpace(ErrorMessage))
            {
                <div class="alert alert-warning mt-3 mb-0" role="alert">@ErrorMessage</div>
            }
        </div>
    </section>
}
else if (IsSetupComplete == true)
{
    <section class="card shadow-sm">
        <div class="card-body">
            <h2 class="h5 card-title">Setup complete</h2>
            <p class="text-secondary mb-0">Setup is already complete.</p>
        </div>
    </section>
}
else if (HasAnyUsers == true)
{
    <section class="card shadow-sm">
        <div class="card-body">
            <h2 class="h5 card-title">Recovery required</h2>
            <p class="text-secondary">Setup is incomplete, but an admin user already exists.</p>
            <a class="btn btn-primary" href="/login">Sign in for recovery</a>
        </div>
    </section>
}
else
{
    <div class="admin-panel-grid">
        <section class="card shadow-sm">
            <form class="card-body" method="post" action="/setup/complete">
                <h2 class="h5 card-title">First admin</h2>

                <div class="mb-3">
                    <label class="form-label" for="setup-username">Username</label>
                    <input id="setup-username" class="form-control" name="username" />
                </div>

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

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

                <h2 class="h5 card-title">Guest access</h2>
                <p class="text-secondary">Set the first code guests use to view the list.</p>

                <div class="mb-3">
                    <label class="form-label" for="setup-access-code">Guest access code</label>
                    <input id="setup-access-code" class="form-control" name="guestAccessCode" />
                </div>

                <button type="submit" class="btn btn-primary">
                    Create and finish setup
                </button>

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

        @if (HasAnyDatabaseBackups == true)
        {
            <section class="card shadow-sm">
                <div class="card-body">
                    <h2 class="h5 card-title">Restore backup</h2>
                    <p class="text-secondary">Restore the latest database backup created by setup reset.</p>
                    <form method="post" action="/setup/restore">
                        <button type="submit" class="btn btn-outline-primary">
                            Restore latest database backup
                        </button>
                    </form>
                </div>
            </section>
        }
    </div>
}
