using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.WebUtilities;

namespace RpgRoller.Components.Pages;

[ExcludeFromCodeCoverage]
public abstract class AuthenticatedPageBase : ComponentBase
{
    protected Task OnLoggedOutAsync(string? message)
    {
        if (string.IsNullOrWhiteSpace(message))
        {
            Navigation.NavigateTo("/login", forceLoad: true);
            return Task.CompletedTask;
        }

        var query = new Dictionary<string, string?>
        {
            ["message"] = message,
            ["kind"] = message.Contains("expired", StringComparison.OrdinalIgnoreCase) ? "error" : "success"
        };

        Navigation.NavigateTo(QueryHelpers.AddQueryString("/login", query), forceLoad: true);
        return Task.CompletedTask;
    }

    [Inject] protected NavigationManager Navigation { get; set; } = null!;
}