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

namespace Indotalent.Features.Thirdparty.Customer;

public static class CustomerEndpoint
{
    public static void MapCustomerEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/customer").WithTags("Customers")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetCustomerListQuery());
            return result.ToApiResponse("Customer list retrieved successfully");
        })
        .WithName("GetCustomerList")
        .WithTags("Customers");

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

        group.MapGet("/lookup", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new LookupCustomerQuery());
            return result.ToApiResponse("Customer lookup data retrieved successfully");
        })
        .WithName("GetCustomerLookup")
        .WithTags("Customers");

        group.MapPost("/", async (CreateCustomerRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateCustomerCommand(request));
            return result.ToApiResponse("Customer has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateCustomer")
        .WithTags("Customers");

        group.MapPost("/update", async (UpdateCustomerRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateCustomerCommand(request));
            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. The customer data could not be found.");
            }
            return result.ToApiResponse("Customer has been updated successfully");
        })
        .WithName("UpdateCustomer")
        .WithTags("Customers");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteCustomerByIdCommand(new DeleteCustomerByIdRequest(id)));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. The customer data could not be found.");
            }
            return true.ToApiResponse("Customer has been deleted successfully");
        })
        .WithName("DeleteCustomerById")
        .WithTags("Customers");

        group.MapPost("/customer-contact", async (CreateCustomerContactRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateCustomerContactCommand(request));
            return result.ToApiResponse("Customer contact has been added successfully");
        })
        .WithName("CreateCustomerContactChild")
        .WithTags("Customers");

        group.MapPost("/customer-contact/update", async (UpdateCustomerContactRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateCustomerContactCommand(request));
            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. Customer contact not found.");
            }
            return result.ToApiResponse("Customer contact has been updated successfully");
        })
        .WithName("UpdateCustomerContactChild")
        .WithTags("Customers");

        group.MapPost("/customer-contact/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteCustomerContactCommand(id));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. Customer contact not found.");
            }
            return true.ToApiResponse("Customer contact has been removed successfully");
        })
        .WithName("DeleteCustomerContactChild")
        .WithTags("Customers");
    }
}