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

namespace Indotalent.Features.Purchase.PurchaseReturn;

public static class PurchaseReturnEndpoint
{
    public static void MapPurchaseReturnEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/purchase-return").WithTags("PurchaseReturns")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

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

        group.MapPost("/", async (CreatePurchaseReturnRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreatePurchaseReturnCommand(request));
            return result.ToApiResponse("Purchase return has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreatePurchaseReturn");

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

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

        group.MapPost("/purchase-return-item", async (CreatePurchaseReturnItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreatePurchaseReturnItemCommand(request));
            return result.ToApiResponse("Item has been added successfully");
        })
        .WithName("CreatePurchaseReturnItem");

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

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