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

namespace Web.Api.Endpoints.Attendance;

internal sealed class CheckIn : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/attendance/check-in", async (
            Application.Features.Api.Attendance.CheckIn.Command body,
            ClaimsPrincipal user,
            ISender sender,
            CancellationToken ct
        ) => {
            var memberID = user.GetMemberID();

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

            var command = body with {
                MemberID = memberID.Value
            };
            var result = await sender.Send(command, ct);

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