using Indotalent.ConfigFrontEnd.Extensions;
using Indotalent.ConfigFrontEnd.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;

namespace Indotalent.ConfigFrontEnd.Common;


public abstract class BaseAppPage : ComponentBase
{
    [Inject]
    protected AuthenticationStateProvider AuthStateProvider { get; set; } = default!;

    [Inject]
    protected CurrentUserState State { get; set; } = default!;

    protected override async Task OnParametersSetAsync()
    {
        await SyncUserContext();
        await base.OnParametersSetAsync();
    }

    private async Task SyncUserContext()
    {
        var authState = await AuthStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;

        if (user.Identity?.IsAuthenticated == true)
        {
            State.UserId = user.GetUserId();
            State.UserName = user.GetUserName();
            State.Email = user.GetEmail();
            State.FullName = user.GetFullName();
            State.TenantId = user.GetTenantId();
        }
    }
}