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

namespace Indotalent.Features.Utilities.BookingResource;

public static class BookingResourceEndpoint
{
    public static void MapBookingResourceEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/booking-resource").WithTags("BookingResources")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

        group.MapGet("/lookup-booking-group", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new LookupBookingGroupQuery());
            return result.ToApiResponse("Booking group lookup retrieved successfully");
        })
        .WithName("GetBookingGroupLookupForResource")
        .WithTags("BookingResources");

        group.MapPost("/", async (CreateBookingResourceRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateBookingResourceCommand(request));
            return result.ToApiResponse("Booking resource has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateBookingResource")
        .WithTags("BookingResources");

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

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