using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Thirdparty.VendorContact.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Thirdparty.VendorContact;

public static class VendorContactEndpoint
{
    public static void MapVendorContactEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/vendor-contact").WithTags("VendorContacts")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetVendorContactListQuery());

            return result.ToApiResponse("Vendor contact list retrieved successfully");
        })
        .WithName("GetVendorContactList")
        .WithTags("VendorContacts");

        group.MapGet("/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetVendorContactByIdQuery(id));

            return result.ToApiResponse(result is not null
                ? "Vendor contact detail retrieved successfully"
                : $"Vendor contact with ID {id} not found");
        })
        .WithName("GetVendorContactById")
        .WithTags("VendorContacts");

        group.MapGet("/lookup", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new LookupVendorContactQuery());

            return result.ToApiResponse("Vendor contact lookup data retrieved successfully");
        })
        .WithName("GetVendorContactLookup")
        .WithTags("VendorContacts");

        group.MapPost("/", async (CreateVendorContactRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateVendorContactCommand(request));

            return result.ToApiResponse("Vendor contact has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateVendorContact")
        .WithTags("VendorContacts");

        group.MapPost("/update", async (UpdateVendorContactRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateVendorContactCommand(request));

            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. The contact data could not be found.");
            }

            return result.ToApiResponse("Vendor contact has been updated successfully");
        })
        .WithName("UpdateVendorContact")
        .WithTags("VendorContacts");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteVendorContactByIdCommand(new DeleteVendorContactByIdRequest(id)));

            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. The contact data could not be found.");
            }

            return true.ToApiResponse("Vendor contact has been deleted successfully");
        })
        .WithName("DeleteVendorContactById")
        .WithTags("VendorContacts");
    }
}