namespace NetinaShop.Api.Controller;

public class UserAddressController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("UserAddress")
            .MapGroup("api/user/address")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

        group.MapGet("", GetAddressesAsync)
            .WithDisplayName("Get Addresses")
            .HasApiVersion(1.0);

        group.MapPost("", PostAddressesAsync)
            .WithDisplayName("Post Addresses")
            .HasApiVersion(1.0);

        group.MapDelete("{id}", DeleteAddressesAsync)
            .WithDisplayName("Delete Address")
            .HasApiVersion(1.0);
    }

    public async Task<IResult> GetAddressesAsync([FromServices] IMediator mediator,CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetUserAddressesQuery(null), cancellationToken));

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

    public async Task<IResult> DeleteAddressesAsync(Guid id, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new DeleteAddressCommand(id), cancellationToken));
}