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

namespace Indotalent.Features.Inventory.TransferIn;

public static class TransferInEndpoint
{
    public static void MapTransferInEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/transfer-in").WithTags("TransferIns")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

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

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

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

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

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

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

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