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

namespace Indotalent.Features.Sales.CreditNote;

public static class CreditNoteEndpoint
{
    public static void MapCreditNoteEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/credit-note").WithTags("CreditNotes")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

        group.MapGet("/sales-return-detail/{srId}", async (string srId, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetSalesReturnDetailForCreditNoteQuery(srId));
            return result.ToApiResponse(result is not null
                ? "Sales return detail for credit note retrieved successfully"
                : $"Sales return with ID {srId} not found");
        })
        .WithName("GetSalesReturnDetailForCreditNote");

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

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

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

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