using Web.Api.Common;
using Web.Api.Extensions;
using MediatR;
using System.Security.Claims;
using Domain.Entities.Wallets.ValueObject;

namespace Web.Api.Endpoints.Wallet;

internal sealed class Transactions : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/wallet/transactions", async (
            ClaimsPrincipal user,
            ISender sender,
            int page = 1,
            ushort perPage = 20,
            WalletTransactionType? type = null,
            CancellationToken ct = default
        ) => {
            var memberID = user.GetMemberID();

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

            var result = await sender.Send(
                new Application.Features.Api.Member.Wallet.GetMyTransactions.Query(memberID.Value, page, perPage, type),
                ct
            );

            return ApiResponse.Ok(result);
        })
        .WithTags("Wallet")
        .RequireAuthorization();
    }
}