@using Microsoft.EntityFrameworkCore
@using OpenArchival.DataAccess
@using MudBlazor

@namespace OpenArchival.Blazor.Blog

<MudPaper Class="pa-4 ma-2 rounded" Elevation="3" Style="overflow-x:auto">
    <MudText Typo="Typo.h6">Latest Blog Posts</MudText>
    <MudDivider/>
    <MudStack Row="true" Spacing="4" Style="flex-wrap: nowrap;" Class="mt-4">
        @foreach (BlogPost post in BlogPosts)
        {
            <div style="min-width: 300px;">
                <BlogPostCard Post="post" Height="300"></BlogPostCard>
            </div>
        }
    </MudStack>
</MudPaper>

@inject IDbContextFactory<ApplicationDbContext> ContextFactory;
@code {
    private List<BlogPost> BlogPosts { get; set; } = [];

    protected override async Task OnInitializedAsync()
    {
        await using var context = await ContextFactory.CreateDbContextAsync();
        BlogPosts = await context.BlogPosts
            .Include(p => p.MainPhoto)
            .OrderByDescending(p => p.CreationTime)
            .Take(10)
            .ToListAsync();
    }
}
