using Application.Abstractions.Messaging;
using Web.Api.Extensions;
using Web.Api.Common;

namespace Web.Api.Endpoints.Auth;

internal sealed class Register : IEndpoint
{
    public sealed record Request(
        string Email,
        string Password,
        string? Name
    );

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        // 회원가입 요청
        app.MapPost("api/auth/register", async (
            Request request,
            ISender sender,
            CancellationToken ct
        ) => {
            var command = new Application.Features.Api.Auth.Register.Command(
                request.Email,
                request.Password,
                request.Name
            );

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

            return result.Match(
                result => ApiResponse.Created(result),
                CustomResults.Problem
            );
        })
        .WithTags("Auth")
        .AllowAnonymous();
    }
}