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

namespace Indotalent.Features.Pipeline.SalesTeam;

public static class SalesTeamEndpoint
{
    public static void MapSalesTeamEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/sales-team").WithTags("SalesTeams")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

            return result.ToApiResponse("Sales team list retrieved successfully");
        })
        .WithName("GetSalesTeamList")
        .WithTags("SalesTeams");

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

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

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

            return result.ToApiResponse("Sales team has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateSalesTeam")
        .WithTags("SalesTeams");

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

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

            return result.ToApiResponse("Sales team has been updated successfully");
        })
        .WithName("UpdateSalesTeam")
        .WithTags("SalesTeams");

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

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

            return true.ToApiResponse("Sales team has been deleted successfully");
        })
        .WithName("DeleteSalesTeamById")
        .WithTags("SalesTeams");

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

            return result.ToApiResponse("Representative has been added to team successfully");
        })
        .WithName("CreateSalesTeamRepresentative")
        .WithTags("SalesTeams");

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

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

            return result.ToApiResponse("Representative has been updated successfully");
        })
        .WithName("UpdateSalesTeamRepresentative")
        .WithTags("SalesTeams");

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

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

            return true.ToApiResponse("Representative has been removed from team successfully");
        })
        .WithName("DeleteSalesTeamRepresentative")
        .WithTags("SalesTeams");
    }
}