using FastEndpoints;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Endpoints.Blog;

public class GetPublishedEndpoint : Endpoint<BlogPublishedQuery, PaginatedResponse<object>>
{
    private readonly BlogService _blogService;

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

    public override void Configure()
    {
        Get("/api/blog");
        AllowAnonymous();
    }

    public override async Task HandleAsync(BlogPublishedQuery request, CancellationToken ct)
    {
        var (items, total) = await _blogService.GetPublishedPagedAsync(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);
    }
}
