using DeepDrftModels.DTOs;
using DeepDrftModels.Enums;
using DeepDrftPublic.Client.Common;
using DeepDrftPublic.Client.Services;
using Microsoft.AspNetCore.Components;
using Models.Common;

namespace DeepDrftPublic.Client.Pages;

/// <summary>
/// Medium-filtered release gallery. Routed at <c>/cuts</c> (Cut releases) and parameterized by
/// <see cref="Medium"/> so the same component can back any medium's card grid without a fork.
/// Cards open the release's dedicated detail page via <see cref="ReleaseRoutes.DetailHref(ReleaseDto)"/>
/// (a Cut routes to <c>/cuts/{entryKey}</c>), the single source for medium→route resolution (Phase 11 §2).
/// </summary>
public partial class AlbumsView : ComponentBase, IDisposable
{
    private const string PersistKeyPrefix = "albums-view-";

    [Inject] public required IReleaseDataService ReleaseData { get; set; }
    [Inject] public required PersistentComponentState PersistentState { get; set; }

    // The medium whose releases this grid shows. Defaults to Cut for the /cuts route; other media
    // can reuse this component by passing a different value. Drives both the fetch filter and the
    // per-medium persistence key so prerendered state never bleeds across media.
    [Parameter] public ReleaseMedium Medium { get; set; } = ReleaseMedium.Cut;

    private bool _loading = true;
    private List<ReleaseDto> _albums = [];
    private PersistingComponentStateSubscription _persistingSubscription;

    private string PersistKey => $"{PersistKeyPrefix}{Medium}";

    protected override async Task OnInitializedAsync()
    {
        // Bridge the prerendered fetch across the prerender -> WASM seam (see MediumBrowseBase).
        // Without this, the WASM pass re-fetches and replays the card entrance animations.
        _persistingSubscription = PersistentState.RegisterOnPersisting(PersistAlbums);

        if (PersistentState.TryTakeFromJson<List<ReleaseDto>>(PersistKey, out var restored) && restored is not null)
        {
            _albums = restored;
            _loading = false;
            return;
        }

        var result = await ReleaseData.GetPaged(Medium.ToString().ToLowerInvariant(), page: 1, pageSize: 100);
        if (result is { Success: true, Value: { Items: { } items } })
            _albums = items.ToList();

        _loading = false;
    }

    private Task PersistAlbums()
    {
        if (_albums.Count > 0)
            PersistentState.PersistAsJson(PersistKey, _albums);
        return Task.CompletedTask;
    }

    // Cut cards show track count where the shared card otherwise shows the artist.
    private static string TrackCountLabel(ReleaseDto album)
        => $"{album.TrackCount} {(album.TrackCount == 1 ? "track" : "tracks")}";

    public void Dispose() => _persistingSubscription.Dispose();
}
