@page "/shared"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
@rendermode InteractiveServer
@inject WishlistService Wishlists
@inject AuthenticationStateProvider AuthState

<PageTitle>Shared With Me</PageTitle>

<h1>Shared With Me</h1>

@if (lists is null)
{
    <p>Loading…</p>
}
else if (lists.Count == 0)
{
    <p class="text-secondary">No wishlists have been shared with you yet.</p>
}
else
{
    <div class="list-group">
        @foreach (var w in lists)
        {
            <a href="@($"wishlist/{w.Id}")" class="list-group-item list-group-item-action">
                <strong>@w.Title</strong>
                <span class="text-secondary">— @(w.Owner?.DisplayName ?? "Unknown")</span>
                <div class="text-secondary small">@w.Description</div>
            </a>
        }
    </div>
}

@code {
    private List<Wishlist>? lists;

    protected override async Task OnInitializedAsync()
    {
        var userId = await AuthState.GetUserIdAsync();
        if (userId is null) return;
        lists = await Wishlists.GetSharedWithAsync(userId);
    }
}
