using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Pipeline.LeadContact.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Pipeline.LeadContact;

public static class LeadContactEndpoint
{
    public static void MapLeadContactEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/lead-contact").WithTags("LeadContacts")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/lookup", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetLeadContactLookupQuery());
            return result.ToApiResponse("Lookup data retrieved successfully");
        })
        .WithName("GetLeadContactLookup");

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetLeadContactListQuery());
            return result.ToApiResponse("Lead contact list retrieved successfully");
        })
        .WithName("GetLeadContactList");

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

        group.MapPost("/", async (CreateLeadContactRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateLeadContactCommand(request));
            return result.ToApiResponse("Lead contact has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateLeadContact");

        group.MapPost("/update", async (UpdateLeadContactRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateLeadContactCommand(request));
            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. The lead contact data could not be found.");
            }
            return result.ToApiResponse("Lead contact has been updated successfully");
        })
        .WithName("UpdateLeadContact");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteLeadContactByIdCommand(new DeleteLeadContactByIdRequest(id)));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. The lead contact data could not be found.");
            }
            return true.ToApiResponse("Lead contact has been deleted successfully");
        })
        .WithName("DeleteLeadContactById");

        group.MapPost("/change-avatar", async (ChangeLeadContactAvatarRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new ChangeLeadContactAvatarCommand(request));
            return result.ToApiResponse(result.Message);
        })
        .WithName("ChangeLeadContactAvatar");

        group.MapGet("/avatar-info/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetLeadContactAvatarInfoQuery(id));
            return result.ToApiResponse(result is not null ? "Avatar info retrieved" : "Not found");
        })
        .WithName("GetLeadContactAvatarInfo");
    }
}