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

namespace Indotalent.Features.Sales.PaymentReceive;

public static class PaymentReceiveEndpoint
{
    public static void MapPaymentReceiveEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/payment-receive").WithTags("PaymentReceives")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

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

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

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

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

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