namespace NetinaShop.Api.Controller;

public class NewsletterMemberController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Newsletter Members").MapGroup("api/newsletter/member");

        group.MapGet("", GetAllMembersAsync)
            .WithDisplayName("Get All Members")
            .HasApiVersion(1.0)
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

        group.MapPost("", PostMemberAsync)
            .WithDisplayName("Post Member")
            .HasApiVersion(1.0);
    }

    public async Task<IResult> GetAllMembersAsync([FromQuery] int page, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetNewsletterMembersQuery(page), cancellationToken));

    public async Task<IResult> PostMemberAsync([FromBody]CreateNewsletterMemberCommand request, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(request, cancellationToken));
}