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

namespace Indotalent.Features.Thirdparty.Vendor;

public static class VendorEndpoint
{
    public static void MapVendorEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/vendor").WithTags("Vendors")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetVendorListQuery());
            return result.ToApiResponse("Vendor list retrieved successfully");
        })
        .WithName("GetVendorList")
        .WithTags("Vendors");

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

        group.MapGet("/lookup", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new LookupVendorQuery());
            return result.ToApiResponse("Vendor lookup data retrieved successfully");
        })
        .WithName("GetVendorLookup")
        .WithTags("Vendors");

        group.MapPost("/", async (CreateVendorRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateVendorCommand(request));
            return result.ToApiResponse("Vendor has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateVendor")
        .WithTags("Vendors");

        group.MapPost("/update", async (UpdateVendorRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateVendorCommand(request));
            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. The vendor data could not be found.");
            }
            return result.ToApiResponse("Vendor has been updated successfully");
        })
        .WithName("UpdateVendor")
        .WithTags("Vendors");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteVendorByIdCommand(new DeleteVendorByIdRequest(id)));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. The vendor data could not be found.");
            }
            return true.ToApiResponse("Vendor has been deleted successfully");
        })
        .WithName("DeleteVendorById")
        .WithTags("Vendors");

        group.MapPost("/vendor-contact", async (CreateVendorContactRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateVendorContactCommand(request));
            return result.ToApiResponse("Vendor contact has been added successfully");
        })
        .WithName("CreateVendorContactChild")
        .WithTags("Vendors");

        group.MapPost("/vendor-contact/update", async (UpdateVendorContactRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateVendorContactCommand(request));
            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. Vendor contact not found.");
            }
            return result.ToApiResponse("Vendor contact has been updated successfully");
        })
        .WithName("UpdateVendorContactChild")
        .WithTags("Vendors");

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