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

namespace Indotalent.Features.Purchase.PurchaseRequisition;

public static class PurchaseRequisitionEndpoint
{
    public static void MapPurchaseRequisitionEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/purchase-requisition").WithTags("PurchaseRequisitions")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

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

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

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

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

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

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

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