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

namespace Web.Api.Endpoints.Auth;

internal sealed class Profile : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/auth/profile", async (
            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.Auth.GetProfile.Query(memberID.Value);
            var result = await sender.Send(query, ct);

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