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

namespace Web.Api.Endpoints.MyPage;

internal sealed class GetComments : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/mypage/comments", async (
            int? page,
            int? perPage,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetMemberID();

            if (memberID is null)
            {
                return ApiResponse.Fail(StatusCodes.Status401Unauthorized, "Invalid token");
            }

            var query = new Application.Features.Api.MyPage.GetComments.Query(
                memberID.Value,
                page ?? 1,
                perPage ?? 20
            );

            var result = await sender.Send(query, ct);

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