using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TaxBaik.Application.Services;
using TaxBaik.Domain.Entities;
using TaxBaik.Web.Services;
namespace TaxBaik.Web.Pages.Admin.Blog;
[Authorize(AuthenticationSchemes = AdminAuthDefaults.Scheme)]
public class IndexModel(BlogService blogService) : PageModel
{
    public List<BlogPost> Items { get; private set; } = [];
    public string? SearchQuery { get; set; }

    public async Task OnGetAsync(string? searchQuery, bool showArchived = false, CancellationToken ct = default)
    {
        SearchQuery = searchQuery;
        var posts = showArchived
            ? await blogService.GetArchivedPagedAsync(1, 100, ct)
            : await blogService.GetAdminPagedAsync(1, 100, ct);
        Items = posts.Item1
            .Where(p => string.IsNullOrWhiteSpace(searchQuery) ||
                        p.Title.Contains(searchQuery, StringComparison.OrdinalIgnoreCase) ||
                        (p.Content?.Contains(searchQuery, StringComparison.OrdinalIgnoreCase) ?? false))
            .ToList();
    }
}
