@page "/register"
@inject IAuthService AuthService
@inject CustomAuthStateProvider AuthStateProvider
@inject NavigationManager Navigation
@inject ISettingsService SettingsService

@rendermode InteractiveServer

<PageTitle>@T("Registrieren - Author Buddy", "Register - Author Buddy")</PageTitle>

<div class="auth-page">
    <div class="auth-card">
        <h2>@T("Registrieren", "Register")</h2>

        <div class="form-group">
            <label for="username">@T("Benutzername:", "Username:")</label>
            <input id="username" @bind="username" @onkeydown="HandleKeyDown" />
        </div>
        <div class="form-group">
            <label for="password">@T("Passwort:", "Password:")</label>
            <input id="password" type="password" @bind="password" @onkeydown="HandleKeyDown" />
        </div>
        <div class="form-group">
            <label for="confirmPassword">@T("Passwort bestätigen:", "Confirm password:")</label>
            <input id="confirmPassword" type="password" @bind="confirmPassword" @onkeydown="HandleKeyDown" />
        </div>
        <div class="auth-actions">
            <button @onclick="DoRegister" disabled="@isBusy">@T("Registrieren", "Register")</button>
            <a href="/login">@T("Zurück zum Login", "Back to login")</a>
        </div>

        @if (!string.IsNullOrEmpty(errorMessage))
        {
            <div class="auth-error">@errorMessage</div>
        }
    </div>
</div>

@code {
    private string _language = "de";
    private string username = string.Empty;
    private string password = string.Empty;
    private string confirmPassword = string.Empty;
    private string errorMessage = string.Empty;
    private bool isBusy;

    private string T(string de, string en) => _language == "en" ? en : de;

    protected override async Task OnInitializedAsync()
    {
        _language = await SettingsService.GetLanguageAsync() ?? "de";
    }

    private async Task HandleKeyDown(KeyboardEventArgs e)
    {
        if (e.Key == "Enter")
            await DoRegister();
    }

    private async Task DoRegister()
    {
        if (string.IsNullOrWhiteSpace(username))
        {
            errorMessage = T("Bitte Benutzername eingeben.", "Please enter a username.");
            return;
        }
        if (string.IsNullOrWhiteSpace(password))
        {
            errorMessage = T("Bitte Passwort eingeben.", "Please enter a password.");
            return;
        }
        if (password != confirmPassword)
        {
            errorMessage = T("Passwörter stimmen nicht überein.", "Passwords do not match.");
            return;
        }
        if (password.Length < 4)
        {
            errorMessage = T("Passwort muss mindestens 4 Zeichen lang sein.", "Password must be at least 4 characters long.");
            return;
        }

        isBusy = true;
        errorMessage = string.Empty;

        try
        {
            var result = await AuthService.RegisterAsync(username, password);
            if (result)
            {
                AuthStateProvider.NotifyStateChanged();
                Navigation.NavigateTo("/");
            }
            else
            {
                errorMessage = T("Benutzername ist bereits vergeben.", "Username is already taken.");
            }
        }
        catch (Exception ex)
        {
            errorMessage = $"{T("Fehler:", "Error:")} {ex.Message}";
        }
        finally
        {
            isBusy = false;
        }
    }
}
