@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@model LiteCharmsSecurity.STS.Identity.ViewModels.Grants.GrantsViewModel

<section class="space-y-6">
  <div class="card">
    <div class="card-header">
      <h2 class="card-title">@Localizer["Title"]</h2>
      <p class="card-description">@Localizer["SubTitle"]</p>
    </div>

    <div class="card-content">
      @if (!Model.Grants.Any())
      {
        <div class="alert-default">
          <lucideicon name="info" class="h-4 w-4" />
          <div>
            <h5 class="alert-title">@Localizer["Info"]</h5>
            <div class="alert-description">@Localizer["NoGrants"]</div>
          </div>
        </div>
      }
      else
      {
        <div class="space-y-4">
          @foreach (var grant in Model.Grants)
          {
            <div class="rounded-lg border bg-card p-4 shadow-sm hover:shadow-md transition-shadow">
              <div class="flex items-start gap-4">
                <div class="flex h-14 w-14 shrink-0 items-center justify-center rounded-lg border bg-background">
                  @if (grant.ClientLogoUrl != null)
                  {
                    <img src="@grant.ClientLogoUrl" alt="@grant.ClientName" class="h-12 w-12 object-contain" />
                  }
                  else
                  {
                    <lucideicon name="app-window" class="h-6 w-6 text-muted-foreground" />
                  }
                </div>

                <div class="min-w-0 flex-1">
                  <h3 class="font-semibold text-base mb-2">@grant.ClientName</h3>

                  <div class="flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-muted-foreground mb-3">
                    <span class="inline-flex items-center gap-1">
                      <lucideicon name="calendar" class="h-3 w-3" />
                      <span class="font-medium">@Localizer["Created"]</span> @grant.Created.ToString("dd MMM yyyy")
                    </span>
                    @if (grant.Expires.HasValue)
                    {
                      <span class="inline-flex items-center gap-1">
                        <lucideicon name="clock" class="h-3 w-3" />
                        <span class="font-medium">@Localizer["Expires"]</span> @grant.Expires.Value.ToString("dd MMM yyyy")
                      </span>
                    }
                  </div>

                  @if (grant.IdentityGrantNames.Any())
                  {
                    <div class="mb-2">
                      <div class="text-xs font-semibold text-muted-foreground mb-1">
                        <lucideicon name="user" class="inline-block h-3 w-3 mr-1" />
                        @Localizer["IdentityGrants"]
                      </div>
                      <div class="flex flex-wrap gap-1">
                        @foreach (var name in grant.IdentityGrantNames) 
                        { 
                          <span class="badge-secondary text-xs">@name</span>
                        }
                      </div>
                    </div>
                  }

                  @if (grant.ApiGrantNames.Any())
                  {
                    <div>
                      <div class="text-xs font-semibold text-muted-foreground mb-1">
                        <lucideicon name="key" class="inline-block h-3 w-3 mr-1" />
                        @Localizer["ApiGrants"]
                      </div>
                      <div class="flex flex-wrap gap-1">
                        @foreach (var name in grant.ApiGrantNames) 
                        { 
                          <span class="badge-secondary text-xs">@name</span>
                        }
                      </div>
                    </div>
                  }
                </div>

                <div class="shrink-0">
                  <form asp-action="Revoke" method="post">
                    <input type="hidden" name="clientId" value="@grant.ClientId" />
                    <button class="btn-destructive btn-sm inline-flex items-center gap-1.5" title="@Localizer["RevokeAccess"]">
                      <lucideicon name="x-circle" class="h-4 w-4" />
                      <span>@Localizer["RevokeAccess"]</span>
                    </button>
                  </form>
                </div>
              </div>
            </div>
          }
        </div>
      }
    </div>
  </div>
</section>
