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

namespace Indotalent.Features.Thirdparty.CustomerContact;

public static class CustomerContactEndpoint
{
    public static void MapCustomerContactEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/customer-contact").WithTags("CustomerContacts")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

            return result.ToApiResponse("Customer contact list retrieved successfully");
        })
        .WithName("GetCustomerContactList")
        .WithTags("CustomerContacts");

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

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

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

            return result.ToApiResponse("Customer contact lookup data retrieved successfully");
        })
        .WithName("GetCustomerContactLookup")
        .WithTags("CustomerContacts");

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

            return result.ToApiResponse("Customer contact has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateCustomerContact")
        .WithTags("CustomerContacts");

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

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

            return result.ToApiResponse("Customer contact has been updated successfully");
        })
        .WithName("UpdateCustomerContact")
        .WithTags("CustomerContacts");

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

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

            return true.ToApiResponse("Customer contact has been deleted successfully");
        })
        .WithName("DeleteCustomerContactById")
        .WithTags("CustomerContacts");
    }
}