@using Microsoft.AspNetCore.Identity
@using SportsDivision.Infrastructure.Identity
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@if (SignInManager.IsSignedIn(User))
{
    var currentUser = await UserManager.GetUserAsync(User);
    var initials = "";
    var displayName = UserManager.GetUserName(User) ?? "User";
    if (currentUser != null)
    {
        var first = !string.IsNullOrEmpty(currentUser.FirstName) ? currentUser.FirstName[0].ToString() : "";
        var last = !string.IsNullOrEmpty(currentUser.LastName) ? currentUser.LastName[0].ToString() : "";
        initials = (first + last).ToUpper();
        if (!string.IsNullOrEmpty(currentUser.FirstName))
        {
            displayName = currentUser.FirstName;
        }
    }
    if (string.IsNullOrEmpty(initials))
    {
        initials = displayName.Length > 0 ? displayName[0].ToString().ToUpper() : "U";
    }

    <div class="dropdown user-dropdown">
        <button class="dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
            <span class="user-avatar">@initials</span>
            <span class="d-none d-sm-inline">@displayName</span>
        </button>
        <ul class="dropdown-menu dropdown-menu-end">
            <li>
                <a class="dropdown-item" asp-controller="Help" asp-action="Index">
                    <i class="bi bi-question-circle me-2"></i>Help &amp; User Guide
                </a>
            </li>
            <li><hr class="dropdown-divider" /></li>
            <li>
                <form asp-controller="Account" asp-action="Logout" method="post">
                    @Html.AntiForgeryToken()
                    <button type="submit" class="dropdown-item"><i class="bi bi-box-arrow-right me-2"></i>Logout</button>
                </form>
            </li>
        </ul>
    </div>
}
else
{
    <a asp-controller="Account" asp-action="Login" class="btn btn-sm btn-outline-primary">
        <i class="bi bi-box-arrow-in-right me-1"></i>Login
    </a>
}
