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

namespace Indotalent.Features.Purchase.PaymentDisburse;

public static class PaymentDisburseEndpoint
{
    public static void MapPaymentDisburseEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/payment-disburse").WithTags("PaymentDisburses")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetPaymentDisburseListQuery());
            return result.ToApiResponse("Payment disburse list retrieved successfully");
        })
        .WithName("GetPaymentDisburseList");

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

        group.MapGet("/bill-detail/{billId}", async (string billId, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetBillDetailForPaymentQuery(billId));
            return result.ToApiResponse(result is not null
                ? "Bill detail for payment retrieved successfully"
                : $"Bill with ID {billId} not found");
        })
        .WithName("GetBillDetailForPayment");

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

        group.MapPost("/", async (CreatePaymentDisburseRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreatePaymentDisburseCommand(request));
            return result.ToApiResponse("Payment disburse has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreatePaymentDisburse");

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

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