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

namespace Indotalent.Features.Purchase.DebitNote;

public static class DebitNoteEndpoint
{
    public static void MapDebitNoteEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/debit-note").WithTags("DebitNotes")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetDebitNoteListQuery());
            return result.ToApiResponse("Debit note list retrieved successfully");
        })
        .WithName("GetDebitNoteList");

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

        group.MapGet("/purchase-return-detail/{prId}", async (string prId, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetPurchaseReturnDetailForDebitNoteQuery(prId));
            return result.ToApiResponse(result is not null
                ? "Purchase return detail for debit note retrieved successfully"
                : $"Purchase return with ID {prId} not found");
        })
        .WithName("GetPurchaseReturnDetailForDebitNote");

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

        group.MapPost("/", async (CreateDebitNoteRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateDebitNoteCommand(request));
            return result.ToApiResponse("Debit note has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateDebitNote");

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

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