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

namespace Indotalent.Features.Pipeline.SalesRepresentative;

public static class SalesRepresentativeEndpoint
{
    public static void MapSalesRepresentativeEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/sales-representative").WithTags("SalesRepresentatives")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

            return result.ToApiResponse("Sales representative list retrieved successfully");
        })
        .WithName("GetSalesRepresentativeList")
        .WithTags("SalesRepresentatives");

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

            return result.ToApiResponse(result is not null
                ? "Sales representative detail retrieved successfully"
                : $"Sales representative with ID {id} not found");
        })
        .WithName("GetSalesRepresentativeById")
        .WithTags("SalesRepresentatives");

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

            return result.ToApiResponse("Sales representative lookup data retrieved successfully");
        })
        .WithName("GetSalesRepresentativeLookup")
        .WithTags("SalesRepresentatives");

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

            return result.ToApiResponse("Sales representative has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateSalesRepresentative")
        .WithTags("SalesRepresentatives");

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

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

            return result.ToApiResponse("Sales representative has been updated successfully");
        })
        .WithName("UpdateSalesRepresentative")
        .WithTags("SalesRepresentatives");

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

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

            return true.ToApiResponse("Sales representative has been deleted successfully");
        })
        .WithName("DeleteSalesRepresentativeById")
        .WithTags("SalesRepresentatives");
    }
}