using Web.Api.Common;
using Web.Api.Extensions;
using MediatR;
using System.Security.Claims;

namespace Web.Api.Endpoints.Forum.Comment;

internal sealed class List : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/forum/posts/{id}/comments", async (
            int id,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct,
            int page = 1,
            ushort perPage = 20,
            int sort = 0
        ) =>
        {
            var memberID = user.GetMemberID();
            var result = await sender.Send(
                new Application.Features.Api.Forum.Comment.List.Query(id, memberID, page, perPage, sort),
                ct
            );

            return result.Match(
                data => ApiResponse.Ok(data),
                CustomResults.Problem
            );
        })
        .WithTags("Forum")
        .AllowAnonymous();
    }
}
