using Domain.Entities.Forum.ValueObject;
using Web.Api.Common;
using Web.Api.Extensions;
using Application.Abstractions.Messaging;
using System.Security.Claims;

namespace Web.Api.Endpoints.Forum.Comment;

internal sealed class Report : IEndpoint
{
    public sealed record Request(
        ReportType Type,
        string? Reason
    );

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/forum/comments/{id}/report", async (
            int id,
            ClaimsPrincipal user,
            Request request,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetMemberID();

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

            var command = new Application.Features.Api.Forum.CommentReport.Create.Command(
                memberID.Value,
                id,
                request.Type,
                request.Reason
            );

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

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