@page "/register"
@inject IAuthService AuthService
@inject CustomAuthStateProvider AuthStateProvider
@inject NavigationManager Navigation
@inject ILocalizationService Loc

@rendermode InteractiveServer

<PageTitle>@Loc["page.title.register"]</PageTitle>

<div class="auth-page">
    <div class="auth-card">
        <div class="auth-logo">
            <img src="/Assets/logo.jpg" alt="Author Buddy" />
        </div>
        <h2>@Loc["register.heading"]</h2>

        <div class="form-group">
            <label for="username">@Loc["register.label.username"]</label>
            <input id="username" @bind="username" @onkeydown="HandleKeyDown" />
        </div>
        <div class="form-group">
            <label for="password">@Loc["register.label.password"]</label>
            <input id="password" type="password" @bind="password" @onkeydown="HandleKeyDown" />
        </div>
        <div class="form-group">
            <label for="confirmPassword">@Loc["register.label.confirm_password"]</label>
            <input id="confirmPassword" type="password" @bind="confirmPassword" @onkeydown="HandleKeyDown" />
        </div>
        <div class="auth-actions">
            <button @onclick="DoRegister" disabled="@isBusy">@Loc["register.button.submit"]</button>
            <a href="/login">@Loc["register.link.back_to_login"]</a>
        </div>

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

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

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

    private async Task DoRegister()
    {
        if (string.IsNullOrWhiteSpace(username))
        {
            errorMessage = Loc["register.error.no_username"];
            return;
        }
        if (string.IsNullOrWhiteSpace(password))
        {
            errorMessage = Loc["register.error.no_password"];
            return;
        }
        if (password != confirmPassword)
        {
            errorMessage = Loc["register.error.password_mismatch"];
            return;
        }
        if (password.Length < 4)
        {
            errorMessage = Loc["register.error.password_too_short"];
            return;
        }

        isBusy = true;
        errorMessage = string.Empty;

        try
        {
            var result = await AuthService.RegisterAsync(username, password);
            if (result)
            {
                AuthStateProvider.NotifyStateChanged();
                Navigation.NavigateTo("/");
            }
            else
            {
                errorMessage = Loc["register.error.username_taken"];
            }
        }
        catch (Exception ex)
        {
            errorMessage = $"{Loc["common.error"]} {ex.Message}";
        }
        finally
        {
            isBusy = false;
        }
    }
}
