using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Sales.DeliveryOrder.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Sales.DeliveryOrder;

public static class DeliveryOrderEndpoint
{
    public static void MapDeliveryOrderEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/delivery-order").WithTags("DeliveryOrders")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetDeliveryOrderListQuery());
            return result.ToApiResponse("Delivery order list retrieved successfully");
        })
        .WithName("GetDeliveryOrderList");

        group.MapGet("/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetDeliveryOrderByIdQuery(id));
            return result.ToApiResponse(result is not null
                ? "Delivery order detail retrieved successfully"
                : $"Delivery order with ID {id} not found");
        })
        .WithName("GetDeliveryOrderById");

        group.MapGet("/lookup", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetDeliveryOrderLookupQuery());
            return result.ToApiResponse("Lookup data retrieved successfully");
        })
        .WithName("GetDeliveryOrderLookup");

        group.MapPost("/", async (CreateDeliveryOrderRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateDeliveryOrderCommand(request));
            return result.ToApiResponse("Delivery order has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateDeliveryOrder");

        group.MapPost("/update", async (UpdateDeliveryOrderRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateDeliveryOrderCommand(request));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Update failed. Delivery order not found.");
            }
            return result.ToApiResponse("Delivery order has been updated successfully");
        })
        .WithName("UpdateDeliveryOrder");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteDeliveryOrderByIdCommand(id));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. Delivery order not found.");
            }
            return true.ToApiResponse("Delivery order has been deleted successfully");
        })
        .WithName("DeleteDeliveryOrderById");

        group.MapPost("/delivery-order-item", async (CreateDeliveryOrderItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateDeliveryOrderItemCommand(request));
            return result.ToApiResponse("Item has been added successfully");
        })
        .WithName("CreateDeliveryOrderItem");

        group.MapPost("/delivery-order-item/update", async (UpdateDeliveryOrderItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateDeliveryOrderItemCommand(request));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Update item failed.");
            }
            return result.ToApiResponse("Item has been updated successfully");
        })
        .WithName("UpdateDeliveryOrderItem");

        group.MapPost("/delivery-order-item/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteDeliveryOrderItemCommand(id));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete item failed.");
            }
            return true.ToApiResponse("Item has been removed successfully");
        })
        .WithName("DeleteDeliveryOrderItem");
    }
}