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

namespace Indotalent.Features.Inventory.StockCount;

public static class StockCountEndpoint
{
    public static void MapStockCountEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/stock-count").WithTags("StockCounts")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetStockCountListQuery());
            return result.ToApiResponse("Stock count list retrieved successfully");
        })
        .WithName("GetStockCountList");

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

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

        group.MapPost("/", async (CreateStockCountRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateStockCountCommand(request));
            return result.ToApiResponse("Stock count has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateStockCount");

        group.MapPost("/update", async (UpdateStockCountRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateStockCountCommand(request));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Update failed. Stock count not found.");
            }
            return result.ToApiResponse("Stock count has been updated successfully");
        })
        .WithName("UpdateStockCount");

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

        group.MapPost("/stock-count-item", async (CreateStockCountItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateStockCountItemCommand(request));
            return result.ToApiResponse("Item has been added successfully");
        })
        .WithName("CreateStockCountItem");

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

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