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

namespace Indotalent.Features.Inventory.Scrapping;

public static class ScrappingEndpoint
{
    public static void MapScrappingEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/scrapping").WithTags("Scrappings")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetScrappingListQuery());
            return result.ToApiResponse("Scrapping list retrieved successfully");
        })
        .WithName("GetScrappingList");

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

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

        group.MapPost("/", async (CreateScrappingRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateScrappingCommand(request));
            return result.ToApiResponse("Scrapping has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateScrapping");

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

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

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

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

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