using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Inventory.TransferOut.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Inventory.TransferOut;

public static class TransferOutEndpoint
{
    public static void MapTransferOutEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/transfer-out").WithTags("TransferOuts")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetTransferOutListQuery());
            return result.ToApiResponse("Transfer out list retrieved successfully");
        })
        .WithName("GetTransferOutList");

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

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

        group.MapPost("/", async (CreateTransferOutRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateTransferOutCommand(request));
            return result.ToApiResponse("Transfer out has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateTransferOut");

        group.MapPost("/update", async (UpdateTransferOutRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateTransferOutCommand(request));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Update failed. Transfer out not found.");
            }
            return result.ToApiResponse("Transfer out has been updated successfully");
        })
        .WithName("UpdateTransferOut");

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

        group.MapPost("/transfer-out-item", async (CreateTransferOutItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateTransferOutItemCommand(request));
            return result.ToApiResponse("Item has been added successfully");
        })
        .WithName("CreateTransferOutItem");

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

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