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

namespace Indotalent.Features.Inventory.PositiveAdjustment;

public static class PositiveAdjustmentEndpoint
{
    public static void MapPositiveAdjustmentEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/positive-adjustment").WithTags("PositiveAdjustments")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

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

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

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

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

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

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

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