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

namespace Web.Api.Endpoints.Forum.Comment;

internal sealed class DownloadFile : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/forum/comment/file/{uuid:guid}", async (
            Guid uuid,
            ClaimsPrincipal user,
            HttpContext httpContext,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetMemberID();

            var command = new Application.Features.Api.Forum.CommentFile.Download.Command(
                uuid,
                memberID,
                httpContext.Connection.RemoteIpAddress?.ToString(),
                httpContext.Request.Headers.UserAgent.ToString()
            );

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

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