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("Get User Current OrderBag")
            .HasApiVersion(1.0);

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

        group.MapPost("add", AddProductToBagAsync)
            .WithDisplayName("Add Product To OrderBag")
            .HasApiVersion(1.0);

        group.MapDelete("remove", RemoveFromOrderBagAsync)
            .WithDisplayName("Remove From OrderBag")
            .HasApiVersion(1.0);

        group.MapPost("submit", SubmitOrderBagAsync)
            .WithDisplayName("Submit OrderBag")
            .HasApiVersion(1.0);


        group.MapPost("discount/{orderId}", DiscountActionOrderBagAsync)
            .WithDisplayName("Add Discount To OrderBag")
            .HasApiVersion(1.0);


        group.MapPost("shipping/{orderId}", AddShippingToOrderBagAsync)
            .WithDisplayName("Add Shipping To OrderBag")
            .HasApiVersion(1.0);

        group.MapPost("payment/{orderId}", SubmitOrderPaymentAsync)
            .WithDisplayName("Submit Order Payment")
            .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> DiscountActionOrderBagAsync(Guid orderId, [FromQuery] string? discountCode, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new SubmitDiscountActionCommand(orderId, discountCode), cancellationToken));

    public async Task<IResult> AddShippingToOrderBagAsync(Guid orderId, [FromBody] SubmitOrderDeliveryCommand request, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(request with { OrderId = orderId }, 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);
    }
}