using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.Blog;

public class GetAdminPagedEndpoint : Endpoint<BlogAdminQuery, PaginatedResponse<object>>
{
    private readonly BlogService _blogService;

    public GetAdminPagedEndpoint(BlogService blogService)
    {
        _blogService = blogService;
    }

    public override void Configure()
    {
        Get("/api/blog/admin");
        Policies("Bearer");
    }

    public override async Task HandleAsync(BlogAdminQuery request, CancellationToken ct)
    {
        var (items, total) = await _blogService.GetAdminPagedAsync(request.Page, request.PageSize);
        await SendAsync(new PaginatedResponse<object>
        {
            Data = items.Cast<object>().ToList(),
            Total = total,
            Page = request.Page,
            PageSize = request.PageSize
        }, 200, cancellation: ct);
    }
}
