@page "/settings/security"
@using AliasVault.Client.Main.Pages.Settings.Security.Components
@inherits MainBase

<LayoutPageTitle>Security settings</LayoutPageTitle>

<PageHeader
    BreadcrumbItems="@BreadcrumbItems"
    Title="Security settings"
    Description="Configure security settings.">
    <CustomActions>
        <RefreshButton OnClick="LoadData" ButtonText="Refresh" />
    </CustomActions>
</PageHeader>

<PasswordChangeSection OnStatusChanged="LoadData" />
<TwoFactorAuthenticationSection @ref="TwoFactorSection" OnStatusChanged="LoadData" />
<QuickVaultUnlockSection @ref="QuickVaultUnlockSection" OnStatusChanged="LoadData" />
<ActiveSessionsSection @ref="SessionsSection" OnSessionsChanged="LoadData" />
<RecentAuthLogsSection @ref="RecentAuthLogsSection" OnSessionsChanged="LoadData" />

@code {
    private TwoFactorAuthenticationSection? TwoFactorSection;
    private QuickVaultUnlockSection? QuickVaultUnlockSection;
    private ActiveSessionsSection? SessionsSection;
    private RecentAuthLogsSection? RecentAuthLogsSection;

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = "Security settings" });
    }

    /// <inheritdoc />
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            await LoadData();
        }
    }

    /// <summary>
    /// Loads data for both the Two-Factor Authentication and Active Sessions sections concurrently.
    /// </summary>
    /// <returns>A task representing the asynchronous operation.</returns>
    private async Task LoadData()
    {
        // Currently with SQLite, we have to load the data sequentially as otherwise we get database locks.
        // When switched over to PostgreSQL, we can load the data concurrently.
        await TwoFactorSection!.LoadData();
        await QuickVaultUnlockSection!.LoadData();
        await SessionsSection!.LoadData();
        await RecentAuthLogsSection!.LoadData();
    }
}
