using NetinaShop.Domain.Enums;

namespace NetinaShop.Api.Controller;

public class OrderBagController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("OrderBag")
            .MapGroup("api/order/bag")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

        group.MapGet("", GetUserCurrentOrderBagAsync)
            .WithDisplayName("GetUserCurrentOrderBagAsync")
            .HasApiVersion(1.0);

        group.MapPost("add/{productId}", AddProductToBagAsync)
            .WithDisplayName("AddProductToBag")
            .HasApiVersion(1.0);

        group.MapPost("remove/{productId}", RemoveFromOrderBagAsync)
            .WithDisplayName("RemoveFromOrderBag")
            .HasApiVersion(1.0);


        group.MapPost("discount/{orderId}", AddDiscountToOrderBagAsync)
            .WithDisplayName("AddDiscountToOrderBag")
            .HasApiVersion(1.0);


        group.MapPost("shipping/{orderId}", AddShippingToOrderBagAsync)
            .WithDisplayName("AddShippingToOrderBag")
            .HasApiVersion(1.0);

        group.MapPost("payment/{orderId}", SubmitOrderPaymentAsync)
            .WithDisplayName("SubmitOrderPayment")
            .HasApiVersion(1.0);
    }

    public async Task<IResult> GetUserCurrentOrderBagAsync(IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetUserOrderBagQuery(), cancellationToken));

    public async Task<IResult> AddProductToBagAsync( Guid productId, [FromQuery]int count, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new AddToOrderBagCommand(productId,count), cancellationToken));


    public async Task<IResult> RemoveFromOrderBagAsync(Guid productId, [FromQuery] int count, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new RemoveFromOrderBagCommand(productId, count), cancellationToken));

    public async Task<IResult> AddDiscountToOrderBagAsync(Guid orderId, [FromQuery] string discountCode, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new SubmitDiscountCommand(orderId, discountCode), cancellationToken));

    public async Task<IResult> AddShippingToOrderBagAsync(Guid orderId, [FromBody] SubmitOrderDeliveryCommand request, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(
            await mediator.Send(new SubmitOrderDeliveryCommand(request.Address, request.PostalCode,
                request.ReceiverPhoneNumber, request.ReceiverFullName, orderId, request.ShippingId), cancellationToken));

    public async Task<IResult> SubmitOrderPaymentAsync(Guid orderId, [FromQuery] OrderPaymentMethod paymentMethod, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok( await mediator.Send(new SubmitOrderPaymentCommand(orderId, paymentMethod), cancellationToken));
}