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

namespace Indotalent.Features.Inventory.NegativeAdjustment;

public static class NegativeAdjustmentEndpoint
{
    public static void MapNegativeAdjustmentEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/negative-adjustment").WithTags("NegativeAdjustments")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetNegativeAdjustmentListQuery());
            return result.ToApiResponse("Negative adjustment list retrieved successfully");
        })
        .WithName("GetNegativeAdjustmentList");

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

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

        group.MapPost("/", async (CreateNegativeAdjustmentRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateNegativeAdjustmentCommand(request));
            return result.ToApiResponse("Negative adjustment has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateNegativeAdjustment");

        group.MapPost("/update", async (UpdateNegativeAdjustmentRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateNegativeAdjustmentCommand(request));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Update failed. Negative adjustment not found.");
            }
            return result.ToApiResponse("Negative adjustment has been updated successfully");
        })
        .WithName("UpdateNegativeAdjustment");

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

        group.MapPost("/negative-adjustment-item", async (CreateNegativeAdjustmentItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateNegativeAdjustmentItemCommand(request));
            return result.ToApiResponse("Item has been added successfully");
        })
        .WithName("CreateNegativeAdjustmentItem");

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

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