using Netina.Domain.Enums;

namespace Netina.Api.Controllers;

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

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

        group.MapPost("check", CheckOrderBagAsync)
            .WithDisplayName("CheckOrderBag")
            .AllowAnonymous()
            .HasApiVersion(1.0);

        group.MapPost("add", AddProductToBagAsync)
            .WithDisplayName("AddProductToBag")
            .HasApiVersion(1.0);

        group.MapDelete("remove", RemoveFromOrderBagAsync)
            .WithDisplayName("RemoveFromOrderBag")
            .HasApiVersion(1.0);

        group.MapPost("submit", SubmitOrderBagAsync)
            .WithDisplayName("SubmitOrderBag")
            .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);
    }

    private async Task<IResult> SubmitOrderBagAsync([FromBody] List<OrderBagRequestDto> requestDtos, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new SubmitOrderBagCommand(requestDtos), cancellationToken));

    private async Task<IResult> CheckOrderBagAsync([FromBody] List<CheckOrderBagRequestItem> request, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new CheckOrderBagCommand(OrderBag: request), cancellationToken));

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

    public async Task<IResult> AddProductToBagAsync([FromBody] List<OrderBagRequestDto> requestDtos, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new AddToOrderBagCommand(requestDtos), cancellationToken));


    public async Task<IResult> RemoveFromOrderBagAsync([FromBody] List<OrderBagRequestDto> requestDtos, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new RemoveFromOrderBagCommand(requestDtos), 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.AddressId, orderId, request.ShippingId), cancellationToken));

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

        return TypedResults.Ok(result);
    }
}