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

namespace Indotalent.Features.Sales.SalesOrder;

public static class SalesOrderEndpoint
{
    public static void MapSalesOrderEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/sales-order").WithTags("SalesOrders")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

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

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

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

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

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

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

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