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

namespace Web.Api.Endpoints.Auth;

internal sealed class GoogleLogin : IEndpoint
{
    public sealed record Request(
        string Credential
    );

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("api/auth/google-login", async (
            Request request,
            HttpContext httpContext,
            ISender sender,
            CancellationToken ct
        ) => {

            var ipAddress = httpContext.GetClientIP();
            var userAgent = httpContext.GetUserAgent();

            var command = new Application.Features.Api.Auth.GoogleLogin.Command(
                request.Credential,
                ipAddress,
                userAgent
            );

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

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