@using BimAI.UI.Shared.Services
@inject AuthService AuthService
@inject NavigationManager Navigation

@if (_isLoading)
{
    <div class="d-flex justify-center align-center" style="height: 100vh;">
        <MudProgressCircular Indeterminate="true" Size="Size.Large" />
    </div>
}
else if (_isAuthenticated)
{
    @ChildContent
}

@code {
    [Parameter] public RenderFragment? ChildContent { get; set; }
    
    private bool _isLoading = true;
    private bool _isAuthenticated = false;
    
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            Console.WriteLine("AuthGuard: Checking authentication...");
            
            _isAuthenticated = await AuthService.CheckAuthenticationAsync();
            _isLoading = false;
            
            Console.WriteLine($"AuthGuard: isAuthenticated={_isAuthenticated}");
            
            if (!_isAuthenticated)
            {
                Console.WriteLine("AuthGuard: Redirecting to /login");
                Navigation.NavigateTo("/login", replace: true);
            }
            
            StateHasChanged();
        }
    }
}