@page "/user/start"
@using AliasVault.Client.Auth.Components
@using AliasVault.Client.Shared.Components
@inherits AliasVault.Client.Auth.Pages.Base.LoginBase
@layout Auth.Layout.EmptyLayout
@inject Config Config
@attribute [AllowAnonymous]
@using Microsoft.Extensions.Localization

<div class="relative">
    <div class="absolute top-4 right-4 z-10">
        <LanguageSwitcher />
    </div>
</div>

<div class="flex flex-col items-center justify-center px-6 pt-8 pb-8 mx-auto md:h-screen pt:mt-0 relative">
    <div class="w-full max-w-7xl mx-auto flex flex-col lg:flex-row bg-gray-100 dark:bg-gray-900">
        <div class="hidden lg:flex lg:w-1/2 items-center justify-center p-8">
            <div class="text-white text-4xl font-bold">
                <img src="img/logo.svg" alt="AliasVault" class="w-64 h-64" />
            </div>
        </div>

        <div class="w-full lg:w-1/2 flex items-center justify-center lg:px-8 lg:py-12 bg-gray-100 dark:bg-gray-900">
            <div class="w-full max-w-xl p-6 space-y-4">
                <Logo />
                <h2 class="text-3xl font-semibold text-gray-800 dark:text-gray-200 mb-6">
                    @Localizer["MainTitle"]
                </h2>
                <p class="text-lg text-gray-600 dark:text-gray-300 mb-8">
                    @Localizer["TaglineText"]
                </p>
                @if (_isHttpWarning)
                {
                    <MessageInfo Title="@Localizer["HttpsWarningTitle"]">
                        @Localizer["HttpsWarningMessage"]
                    </MessageInfo>
                }
                <div class="space-y-4">
                    @if (Config.PublicRegistrationEnabled)
                    {
                        <a href="/user/setup" class="block w-full py-3 px-4 bg-primary-600 hover:bg-primary-700 text-white font-semibold rounded-lg transition duration-300 ease-in-out text-center">
                            @Localizer["CreateNewVaultButton"]
                        </a>
                    }
                    <a href="/user/login" class="block w-full py-3 px-4 bg-gray-200 hover:bg-gray-300 dark:bg-gray-700 dark:hover:bg-gray-800 text-gray-800 dark:text-white font-semibold rounded-lg transition duration-300 ease-in-out text-center">
                        @Localizer["LoginExistingAccountButton"]
                    </a>
                </div>
            </div>
        </div>

        <FooterLogin />
    </div>
</div>

@code {
    private IStringLocalizer Localizer => LocalizerFactory.Create("Pages.Auth.Start", "AliasVault.Client");
    private bool _isHttpWarning = false;

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        await AuthStateProvider.GetAuthenticationStateAsync();
        var authState = await AuthStateProvider.GetAuthenticationStateAsync();
        if (authState.User.Identity?.IsAuthenticated == true) {
            // Already authenticated, redirect to home page.
            NavigationManager.NavigateTo("/");
        }

        CheckHttpProtocol();
    }

    /// <summary>
    /// Checks if the current URL is using HTTP and shows warning if needed.
    /// Only shows warning for non-localhost hostnames since browsers allow crypto operations on localhost via HTTP.
    /// </summary>
    private void CheckHttpProtocol()
    {
        var uri = new Uri(NavigationManager.Uri);
        var isLocalhost = uri.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase) ||
                         uri.Host.Equals("127.0.0.1", StringComparison.OrdinalIgnoreCase) ||
                         uri.Host.Equals("::1", StringComparison.OrdinalIgnoreCase);
        _isHttpWarning = uri.Scheme == "http" && !isLocalhost;
    }
}
