using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Utilities.BookingManager.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Utilities.BookingManager;

public static class BookingManagerEndpoint
{
    public static void MapBookingManagerEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/booking-manager").WithTags("BookingManager")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetBookingListQuery());
            return result.ToApiResponse("Booking list retrieved successfully");
        })
        .WithName("GetBookingList")
        .WithTags("BookingManager");

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

        group.MapGet("/lookup-data", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new LookupBookingDataQuery());
            return result.ToApiResponse("Booking lookup data retrieved successfully");
        })
        .WithName("GetBookingLookupData")
        .WithTags("BookingManager");

        group.MapPost("/", async (CreateBookingRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateBookingCommand(request));
            return result.ToApiResponse("Booking has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateBooking")
        .WithTags("BookingManager");

        group.MapPost("/update", async (UpdateBookingRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateBookingCommand(request));
            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. The booking data could not be found.");
            }
            return result.ToApiResponse("Booking has been updated successfully");
        })
        .WithName("UpdateBooking")
        .WithTags("BookingManager");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteBookingByIdCommand(new DeleteBookingByIdRequest(id)));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. The booking data could not be found.");
            }
            return true.ToApiResponse("Booking has been deleted successfully");
        })
        .WithName("DeleteBookingById")
        .WithTags("BookingManager");
    }
}