using Insight.Infrastructure.Entities;
using Insight.Infrastructure.Services;
using Insight.Web.Constants;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using MudBlazor;

namespace Insight.Web.Pages.Account;

[Route(Navigation.Account.Profile)]
public partial class Profile
{
    [Parameter] public InsightUser? Account { get; set; }

    [Inject] private IdentityService IdentityService { get; init; } = default!;
    [Inject] private AuthenticationStateProvider AuthenticationStateProvider { get; init; } = default!;

    private readonly string _title = "Profile｜Insight";
    private readonly List<BreadcrumbItem> _breadcrumbs = new()
    {
        new BreadcrumbItem("Home", href: Navigation.Home),
        new BreadcrumbItem("Account", href: Navigation.Account.Profile),
        new BreadcrumbItem("Profile", href: "#", true)
    };

    protected override async Task OnInitializedAsync()
    {
        await OnRefreshAsync();
    }

    private async Task OnRefreshAsync()
    {
        var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        if (state?.User?.Identity?.Name is null) return;

        Account = await IdentityService.GetByEmailAsync(state.User.Identity.Name).ConfigureAwait(false);

        await InvokeAsync(StateHasChanged);
    }
}