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

namespace Web.Api.Endpoints.MyPage;

/// <summary>충전 내역 조회</summary>
internal sealed class ChargeLogs : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        /// 기간별 충전 완료 내역 (today/week/month/quarter/half, 페이징)
        app.MapGet("api/mypage/charge-logs", async (
            string? type,
            int? page,
            int? perPage,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetRequiredMemberID();
            var searchType = type ?? SearchDateType.Today;

            if (!SearchDateType.ValidTypes.Contains(searchType))
            {
                return ApiResponse.Fail(StatusCodes.Status400BadRequest, "Invalid type");
            }

            var query = new Application.Features.Api.MyPage.ChargeLogs.Query(
                memberID,
                searchType,
                page ?? 1,
                perPage ?? 20
            );

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

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