@page "/"
@using DeepDrftManager.Services
@attribute [Authorize]
@layout Layout.CmsLayout
@inject NavigationManager Nav
@inject ICmsTrackService CmsTrackService
@inject ILogger<Index> Logger

<PageTitle>DeepDrft CMS</PageTitle>

<MudContainer MaxWidth="MaxWidth.Large" Class="mt-8">
    <MudText Typo="Typo.h3" Class="mb-6">Catalogue</MudText>

    <MudGrid Spacing="4">
        <MudItem xs="12" sm="4">
            @SummaryCard("Tracks", Icons.Material.Filled.LibraryMusic, Color.Primary, _tracksLoading, _trackCount)
        </MudItem>
        <MudItem xs="12" sm="4">
            @SummaryCard("Releases", Icons.Material.Filled.Album, Color.Secondary, _albumsLoading, _albumCount)
        </MudItem>
        <MudItem xs="12" sm="4">
            @SummaryCard("Genres", Icons.Material.Filled.Category, Color.Tertiary, _genresLoading, _genreCount)
        </MudItem>
    </MudGrid>
</MudContainer>

@code {
    private bool _tracksLoading = true;
    private bool _albumsLoading = true;
    private bool _genresLoading = true;

    private int? _trackCount;
    private int? _albumCount;
    private int? _genreCount;

    protected override async Task OnInitializedAsync()
    {
        // Three independent reads run concurrently. Each loader calls StateHasChanged in its
        // finally block so its card updates as soon as its own fetch returns.
        await Task.WhenAll(LoadTrackCount(), LoadAlbumCount(), LoadGenreCount());
    }

    private async Task LoadTrackCount()
    {
        try
        {
            var result = await CmsTrackService.GetTrackCountAsync();
            _trackCount = result.Success ? result.Value : null;
            if (!result.Success)
            {
                Logger.LogWarning("Dashboard track count failed: {Error}",
                    result.Messages.FirstOrDefault()?.Message ?? "Unknown error");
            }
        }
        finally
        {
            _tracksLoading = false;
            StateHasChanged();
        }
    }

    private async Task LoadAlbumCount()
    {
        try
        {
            var result = await CmsTrackService.GetReleasesAsync();
            _albumCount = result.Success && result.Value is not null ? result.Value.Count : null;
            if (!result.Success)
            {
                Logger.LogWarning("Dashboard album summaries failed: {Error}",
                    result.Messages.FirstOrDefault()?.Message ?? "Unknown error");
            }
        }
        finally
        {
            _albumsLoading = false;
            StateHasChanged();
        }
    }

    private async Task LoadGenreCount()
    {
        try
        {
            var result = await CmsTrackService.GetGenreSummariesAsync();
            _genreCount = result.Success && result.Value is not null ? result.Value.Count : null;
            if (!result.Success)
            {
                Logger.LogWarning("Dashboard genre summaries failed: {Error}",
                    result.Messages.FirstOrDefault()?.Message ?? "Unknown error");
            }
        }
        finally
        {
            _genresLoading = false;
            StateHasChanged();
        }
    }

    private RenderFragment SummaryCard(string label, string icon, Color color, bool loading, int? count) => __builder =>
    {
        <MudCard Elevation="8" Style="height: 100%;">
            <MudCardContent>
                <MudStack AlignItems="AlignItems.Center" Spacing="2" Class="py-4">
                    <MudIcon Icon="@icon" Color="@color" Size="Size.Large" />
                    @if (loading)
                    {
                        <MudProgressCircular Color="@color" Indeterminate="true" Size="Size.Small" />
                    }
                    else
                    {
                        <MudText Typo="Typo.h3" Color="@color">@(count?.ToString() ?? "—")</MudText>
                    }
                    <MudText Typo="Typo.subtitle1" Class="mud-text-secondary text-uppercase">@label</MudText>
                </MudStack>
            </MudCardContent>
            <MudCardActions Class="justify-center pb-4">
                <MudButton Variant="Variant.Text" Color="@color" EndIcon="@Icons.Material.Filled.ArrowForward"
                           OnClick="@(() => Nav.NavigateTo("/tracks"))">
                    View
                </MudButton>
            </MudCardActions>
        </MudCard>
    };
}
