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

namespace Web.Api.Endpoints.Forum.Post;

internal sealed class ClickLink : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/forum/posts/links/{id}/click", async (
            int id,
            ClaimsPrincipal user,
            HttpContext httpContext,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetMemberID();

            var command = new Application.Features.Api.Forum.PostLink.Click.Command(
                id,
                memberID,
                httpContext.GetClientIP(),
                httpContext.GetUserAgent()
            );

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

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