using Web.Api.Common;
using Web.Api.Extensions;
using Application.Abstractions.Messaging;
using System.Security.Claims;

namespace Web.Api.Endpoints.Forum.Post;

internal sealed class Get : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/forum/posts/{id}", async (
            int id,
            ClaimsPrincipal user,
            HttpContext httpContext,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var memberID = user.GetMemberID();
            var ipAddress = httpContext.Connection.RemoteIpAddress?.ToString();
            var result = await sender.Send(new Application.Features.Api.Forum.Post.Get.Query(id, memberID, ipAddress), ct);

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