using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Purchase.PurchaseOrder.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Purchase.PurchaseOrder;

public static class PurchaseOrderEndpoint
{
    public static void MapPurchaseOrderEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/purchase-order").WithTags("PurchaseOrders")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

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

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

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

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

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

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

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