using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Purchase.GoodsReceive.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Purchase.GoodsReceive;

public static class GoodsReceiveEndpoint
{
    public static void MapGoodsReceiveEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/goods-receive").WithTags("GoodsReceives")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetGoodsReceiveListQuery());
            return result.ToApiResponse("Goods receive list retrieved successfully");
        })
        .WithName("GetGoodsReceiveList");

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

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

        group.MapPost("/", async (CreateGoodsReceiveRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateGoodsReceiveCommand(request));
            return result.ToApiResponse("Goods receive has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateGoodsReceive");

        group.MapPost("/update", async (UpdateGoodsReceiveRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateGoodsReceiveCommand(request));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Update failed. Goods receive not found.");
            }
            return result.ToApiResponse("Goods receive has been updated successfully");
        })
        .WithName("UpdateGoodsReceive");

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

        group.MapPost("/inventory-transaction", async (CreateGoodsReceiveItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateGoodsReceiveItemCommand(request));
            return result.ToApiResponse("Item has been added successfully");
        })
        .WithName("CreateGoodsReceiveItem");

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

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