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

namespace Indotalent.Features.Purchase.Bill;

public static class BillEndpoint
{
    public static void MapBillEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/bill").WithTags("Bills")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetBillListQuery());
            return result.ToApiResponse("Bill list retrieved successfully");
        })
        .WithName("GetBillList");

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

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

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

        group.MapPost("/", async (CreateBillRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateBillCommand(request));
            return result.ToApiResponse("Bill has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateBill");

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

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