@page "/account/confirm-email"
@layout AuthenticationLayout
@using Indotalent.Shared.Consts
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.WebUtilities
@using System.Text
@using Indotalent.Data.Entities
@inject UserManager<ApplicationUser> UserManager
@inject NavigationManager Navigation
@inject ISnackbar Snackbar

<PageTitle>Confirming Email - @GlobalConsts.AppInitial</PageTitle>

<style>
    /* ===== CONFIRM CONTAINER ===== */
    .confirm-container {
        display: flex;
        flex-direction: column;
        align-items: center;
        text-align: center;
        padding: 1.5rem 0;
    }

    .confirm-icon-wrapper {
        display: flex;
        justify-content: center;
        width: 100%;
        margin-bottom: 1.5rem;
    }

    .confirm-icon-circle {
        width: 5rem;
        height: 5rem;
        border-radius: 50%;
        background: #f5f3ff;
        border: 4px solid #e8e5ff;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
    }

    .confirm-icon-circle::before {
        content: '';
        position: absolute;
        inset: -8px;
        border-radius: 50%;
        border: 2px solid #ddd6fe;
        opacity: 0.4;
    }

    .confirm-title {
        font-size: 1.25rem;
        font-weight: 700;
        color: #0f172a;
        margin-bottom: 0.5rem;
        width: 100%;
        text-align: center;
    }

    .confirm-desc {
        font-size: 0.875rem;
        color: #64748b;
        margin-bottom: 1.5rem;
        width: 100%;
        text-align: center;
    }
</style>

<div class="confirm-container">

    <div class="confirm-icon-wrapper">
        <div class="confirm-icon-circle">
            <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="#6366F1" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" />
                <polyline points="22 4 12 14.01 9 11.01" />
            </svg>
        </div>
    </div>

    <h2 class="confirm-title">Verifying Your Account</h2>
    <p class="confirm-desc">Please wait while we validate your email address...</p>

    <MudProgressCircular Color="Color.Primary" Indeterminate="true" Size="Size.Large" />

</div>

@code {
    [SupplyParameterFromQuery] public string? userId { get; set; }
    [SupplyParameterFromQuery] public string? code { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(code))
        {
            Snackbar.Add("Invalid or expired confirmation link.", Severity.Error);
            Navigation.NavigateTo("/account/login");
            return;
        }

        var user = await UserManager.FindByIdAsync(userId);
        if (user == null)
        {
            Snackbar.Add("User record not found.", Severity.Error);
            Navigation.NavigateTo("/account/login");
            return;
        }

        try
        {
            var decodedCode = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
            var result = await UserManager.ConfirmEmailAsync(user, decodedCode);
            await Task.Delay(500);
            if (result.Succeeded)
            {
                Snackbar.Add("Email verified successfully! You can now sign in.", Severity.Success);
            }
            else
            {
                Snackbar.Add("Verification failed. The link may have expired.", Severity.Error);
            }
        }
        catch
        {
            Snackbar.Add("An error occurred during verification.", Severity.Error);
        }

        Navigation.NavigateTo("/account/login");
    }
}