@using MemberCenter.Application.Constants
@using MemberCenter.Application.Abstractions
@inject IAdminPermissionChecker AdminPermissionChecker

@{
    var currentArea = ViewContext.RouteData.Values["area"]?.ToString() ?? string.Empty;
    var currentController = ViewContext.RouteData.Values["controller"]?.ToString() ?? string.Empty;
    var currentAction = ViewContext.RouteData.Values["action"]?.ToString() ?? string.Empty;
    var canUseAdmin = User.IsInRole(AdminPermissions.AdminRole) || User.IsInRole(AdminPermissions.SuperuserRole);

    string NavClass(string area, string controller, string action = "Index")
    {
        var isActive =
            string.Equals(currentArea, area, StringComparison.OrdinalIgnoreCase)
            && string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase)
            && string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase);

        return isActive ? "app-nav-link is-active" : "app-nav-link";
    }
}

<aside class="app-sidebar">
  <a class="app-brand" asp-area="" asp-controller="Home" asp-action="Index">
    <img class="app-brand-logo" src="~/images/innovedus-logo.svg" alt="Innovedus" />
  </a>

  <div class="app-sidebar-nav">
    @if (canUseAdmin)
    {
      <section class="app-nav-section" aria-labelledby="admin-nav-heading">
        <h2 id="admin-nav-heading" class="app-nav-heading">@L["Admin"]</h2>
        <nav class="app-nav-list" aria-label="@L["Admin"]">
          @foreach (var item in new[]
          {
              new { Permission = AdminPermissions.Home, Controller = "Home", Label = L["Home"] },
              new { Permission = AdminPermissions.AccountsIndex, Controller = "Accounts", Label = L["Member Management"] },
              new { Permission = AdminPermissions.TenantsIndex, Controller = "Tenants", Label = L["Tenants"] },
              new { Permission = AdminPermissions.NewsletterListsIndex, Controller = "NewsletterLists", Label = L["Newsletter Lists"] },
              new { Permission = AdminPermissions.SubscriptionsIndex, Controller = "Subscriptions", Label = L["Subscriptions"] },
              new { Permission = AdminPermissions.OAuthClientsIndex, Controller = "OAuthClients", Label = L["OAuth Clients"] },
              new { Permission = AdminPermissions.AuditLogsIndex, Controller = "AuditLogs", Label = L["Audit Logs"] },
              new { Permission = AdminPermissions.SecurityIndex, Controller = "Security", Label = L["Security Settings"] },
              new { Permission = AdminPermissions.BlacklistIndex, Controller = "Blacklist", Label = L["Blacklist"] }
          })
          {
              if (await AdminPermissionChecker.HasPermissionAsync(User, item.Permission))
              {
                <a class="@NavClass("Admin", item.Controller)" asp-area="Admin" asp-controller="@item.Controller" asp-action="Index">
                  <span class="app-nav-marker" aria-hidden="true"></span>
                  <span>@item.Label</span>
                </a>
              }
          }
        </nav>
      </section>
    }

    <section class="app-nav-section" aria-labelledby="member-nav-heading">
      <h2 id="member-nav-heading" class="app-nav-heading">@L["Member Profile"]</h2>
      <nav class="app-nav-list" aria-label="@L["Member Profile"]">
        <a class="@NavClass("", "Profile")" asp-area="" asp-controller="Profile" asp-action="Index">
          <span class="app-nav-marker" aria-hidden="true"></span>
          <span>@L["Profile"]</span>
        </a>
        <a class="@NavClass("", "Profile", "Notifications")" asp-area="" asp-controller="Profile" asp-action="Notifications">
          <span class="app-nav-marker" aria-hidden="true"></span>
          <span>@L["Notifications"]</span>
        </a>
        <a class="@NavClass("", "Profile", "Subscriptions")" asp-area="" asp-controller="Profile" asp-action="Subscriptions">
          <span class="app-nav-marker" aria-hidden="true"></span>
          <span>@L["Newsletter Subscriptions"]</span>
        </a>
        @if (User.IsInRole(AdminPermissions.TenantManagerRole))
        {
          <a class="@NavClass("", "TenantNewsletters")" asp-area="" asp-controller="TenantNewsletters" asp-action="Index">
            <span class="app-nav-marker" aria-hidden="true"></span>
            <span>@L["Newsletter Management"]</span>
          </a>
        }
      </nav>
    </section>
  </div>

  <div class="app-sidebar-footer">
    @if (User.Identity?.IsAuthenticated ?? false)
    {
      <div class="app-user-name">@User.Identity!.Name</div>
      <form method="post" asp-area="" asp-controller="Account" asp-action="Logout">
        <button type="submit" class="app-logout-button">@L["Logout"]</button>
      </form>
    }
    else
    {
      <a class="app-logout-button" asp-area="" asp-controller="Account" asp-action="Login">@L["Login"]</a>
    }
  </div>
</aside>
