using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Sales.SalesReturn.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Sales.SalesReturn;

public static class SalesReturnEndpoint
{
    public static void MapSalesReturnEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/sales-return").WithTags("SalesReturns")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

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

        group.MapPost("/", async (CreateSalesReturnRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateSalesReturnCommand(request));
            return result.ToApiResponse("Sales return has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateSalesReturn");

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

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

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

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

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