@page "/register"

@* Virtual route to trick blazor *@

@using Moonlight.Core.Models.Abstractions
@using Moonlight.Core.Models.Forms
@using Moonlight.Core.Services
@using MoonCore.Exceptions
@using MoonCoreUI.Services

@inject IAuthenticationProvider AuthenticationProvider
@inject IdentityService IdentityService
@inject CookieService CookieService
@inject NavigationManager Navigation

<div class="d-flex justify-content-center">
    <div class="d-flex align-items-center">
        <div class="card card-body m-15 p-15">
            <div class="text-center mb-8">
                <div class="fw-bold mb-3 fs-1">
                    Register
                </div>
                <div class="text-gray-400 fw-semibold fs-4">
                    Register in order to start managing your services
                </div>
            </div>

            <SmartForm Model="Form" OnValidSubmit="OnValidSubmit">
                <div class="fv-row mb-8">
                    <input @bind="Form.Username" type="text" placeholder="Username" class="form-control form-control-solid">
                </div>

                <div class="fv-row mb-8">
                    <input @bind="Form.Email" type="text" placeholder="Email" class="form-control form-control-solid">
                </div>

                <div class="fv-row mb-7">
                    <input @bind="Form.Password" type="password" placeholder="Password" class="form-control form-control-solid">
                </div>

                <div class="fv-row mb-7">
                    <input @bind="Form.RepeatedPassword" type="password" placeholder="Repeat your password" class="form-control form-control-solid">
                </div>

                <div class="d-flex flex-stack flex-wrap gap-3 fs-base fw-semibold mb-10">
                    <div></div>
                    <a href="/login" class="link-primary">
                        Already have an account ?
                    </a>
                </div>

                <div class="d-flex flex-stack">
                    <button type="submit" class="btn btn-primary me-2 flex-shrink-0">Sign Up</button>
                    <div class="d-flex align-items-center">
                        <div class="text-gray-400 fw-semibold fs-6 me-3 me-md-6">Or</div>
                        @* OAuth2 Providers here *@
                    </div>
                </div>
            </SmartForm>
        </div>
    </div>
</div>

@code
{
    private RegisterForm Form = new();

    private async Task OnValidSubmit()
    {
        if (Form.Password != Form.RepeatedPassword)
            throw new DisplayException("The passwords do not match");
        
        var user = await AuthenticationProvider.Register(
            Form.Username,
            Form.Email,
            Form.Password
        );

        if (user == null)
            throw new DisplayException("Unable to create account");

        // Generate token and authenticate
        var token = await IdentityService.Authenticate(user);

        // Save token for later use
        await CookieService.SetValue("token", token);
        
        // Forward the user if not on the specific page
        if(new Uri(Navigation.Uri).LocalPath.StartsWith("/register"))
            Navigation.NavigateTo("/");
    }
}