using FastEndpoints;

namespace SpaceGame.Api.Auth.Api;

public sealed class LoginHandler(AuthService authService) : Endpoint<LoginRequest, AuthSessionResponse>
{
    public override void Configure()
    {
        Post("/api/auth/login");
        AllowAnonymous();
    }

    public override async Task HandleAsync(LoginRequest request, CancellationToken cancellationToken)
    {
        try
        {
            await SendOkAsync(await authService.LoginAsync(request, cancellationToken), cancellationToken);
        }
        catch (InvalidOperationException ex)
        {
            AddError(ex.Message);
            await SendErrorsAsync(cancellation: cancellationToken);
        }
    }
}
